Is there an easier way to write this html/razor code - asp.net-mvc-3

<ul>
#{int i=0;}
#foreach (var entry in Model.PhoneNumberEntries)
{
<li>
<span>#entry.PhoneNumber<span>
#Html.TextBoxFor(m=>Model.PhoneNumberEntries[i].PhoneNumber)</li>
i++;
}
</ul>
This seems a little too verbose for me... is there way to get around creating the counter with having to resort to the standard for loop?

This seems a little too verbose for me... is there way to get around
creating the counter with having to resort to the standard for loop?
Yeah it seems too verbose to me as well. Even the loop seems too verbose as you don't need it if you use editor templates.
<ul>
#Html.EditorFor(x => x.PhoneNumberEntries)
</ul>
and then obviously you would define a custom editor template that will automatically be rendered for each element of the PhoneNumberEntries collection (~/Views/Shared/EditorTemplates/PhoneNumberEntry.cshtml):
#model PhoneNumberEntry
<li>
<span>#Model.PhoneNumber</span>
#Html.EditorFor(m => m.PhoneNumber)
</li>
You don't even need to write loops as templates work by convention.
Notice that the name and the location of the editor template is important. It should be located either inside ~/Views/Shared/EditorTemplates if you want to share this template between views belonging to different controllers in your application and it is the location where ASP.NET MVC will first look for it. Or you could also put it inside ~/Views/XXX/EditorTemplates where XXX is the name of the current controller. Then name of the editor template must be the name of the type used as agrument for the collection property.
So if you had no your main view model:
public IEnumerable<FooBarViewModel> FooBars { get; set; }
the name of the corresponding template would be FooBarViewModel.cshtml and obviously it will be strongly typed to FooBarViewModel.

Try this:
<ul>
#for (int i=0; i++; i < Model.PhoneNumberEntries.Count)
{
<li>#Html.TextBoxFor(m=>Model.PhoneNumberEntries[i].PhoneNumber)</li>
}
</ul>

Related

Laravel 4 - understanding View::share()

From what I understand:
View::share('foo','bar');
Will make $foo available in all views.
However, is it correct to say View::share() can be used only in the __construct()?
Because from outside __construct() I can't make it to work.
View::share should be available anywhere within your application. A common place that it is used is in view composers, but it should be usable within a route or wherever you need it.
Yes, adding:
View::share('foo','bar');
in your routes.php file will make $foo (with a value of 'bar') available in all views. This is especially useful for something like Twitter Bootstrap's "active" navigation classes. For example, you could do:
View::share('navactive', '');
to make sure the navactive variable is set in all views (and thus won't throw errors) and then when you are making views (in your controller, for example), you could pass:
return View::make('one')->with('navactive', 'one');
and then in your view (preferably some bootstrappy blade template) you can do the following:
<ul class="nav">
#if ( Auth::user() )
<li #if ($navactive === 'one') class="active" #endif>One</li>
<li #if ($navactive === 'three') class="active" #endif>Three</li>
<li #if ($navactive === 'five') class="active" #endif>Five</li>
#endif
</ul>
Basically if you want to share the variables through all view, you might first want to create a base route(E.x.:internalController.php) as a parent class then extend other controllers as a child of it(E.x:childController.php).
And yeah you will most likely set the view::share('foo', $bar) in the __constructor() of the internalController.php, since it lunches whenever the class is initialized, this way the parent class will serve the variable values to the child classes.

Is there any way to change text color at controller method in MVC3?

Hello everyone I'm trying to do change text color in my controller. Yes I did it but I have to send two parameter to my view. So is there any alternative way to change color in controller method ?
Here is my controller:
public action Test ()
{
ViewBag.stackoverflow = "It's gonna be red";
ViewBag.color = "red";
}
And my view:
#{
ViewBag.Title = "Test me";
}
<font color="#ViewBag.color">#ViewBag.stackoverflow</font>
I was just trying to figure out this same thing. All I did was wrap my ViewBag in view with a bootstrap class:
<div class="text-danger">
#ViewBag.Message //Message comes out red
</div>
In ASP.NET MVC, Controller and View communicate via Model that you send to view. So there is no way.
But it is good thing. MVC introduces separation of various concerns. One of many advantages is testability.
One more thing. your "color"property on ViewBag is not good idea neither. Try to describe the purpose of highlighted value like "priority" or something else and later in view you can decide the color to use for different levels of priority.
I found using the tag with the class from bootstrap helped. Very easy:
<span class="alert-success">
#Html.Raw(TempData["Message"])
</span>

Umbraco Beginning Razor - list all news items

Hi I have a node events and have 6 types 'eventItem' which are children. On my homepage I want to list all nodes of type eventItem and list the eventDate.
Thanks
Using razor you could do something like the following:
#{
dynamic eventFolder = Library.NodeById(1234);
<ul>
#foreach (var event in eventFolder.Children)
{
<li>#event.eventDate.ToShortDateString() - #event.Name</li>
}
</ul>
}
Obviously, replace "1234" with the actual Id of the event folder and then place the razor script inside a macro and put the macro on your home page's template and you should be good to go.

Using ActionLInk from inside a helper template cshtml page

Why doesn't Html.ActionLink work in the below code? This is a page in the app_code folder,
that I am trying to call from index.cshtml
LogOnUserControl.cshtml
#helper DisplayUserControl(){
if (Request.IsAuthenticated ) {
<span>Welcome <strong>#User.Identity.Name</strong>!</span>
<span>[ {#Html.ActionLink("","","")} ]</span>
}
else {
<span>[{#Html.ActionLink("","","") }]</span>
}
}
this is the line of code from index.cshtml. The call itself works, if I remove the Html.ActionLink statements the site loads fine. Is it that you can't use them in a nested page like this? How else can I generate dynamic links?
index.cshtml
#LogOnUserControl.DisplayUserControl()
What's the idea with this action links? Why are you passing empty strings as arguments? I suppose you want to generate SignIn, SignOut links, don't you?
Also if you want to use HTML helpers inside shared helpers that you put in the App_Code folder you will need to pass them as arguments because they are not available:
#using System.Web.Mvc.Html
#helper DisplayUserControl(System.Web.Mvc.HtmlHelper html) {
if (html.ViewContext.HttpContext.User.Identity.IsAuthenticated) {
<span>
Welcome
<strong>
#html.ViewContext.HttpContext.User.Identity.Name
</strong>
!
</span>
<span>[#html.ActionLink("SignOut", "Login")]</span>
}
else {
<span>[#html.ActionLink("SignIn", "Login")]</span>
}
}
and to call the helper:
#LogOnUserControl.DisplayUserControl(Html)
Personally I never use such helpers (the ones you put in the App_Code folder). Can't see any use for them when you have partial views, editor/display templates and Html.Action helpers.
So for example you could define a partial (~/Views/Shared/_LogOnUserControl.cshtml):
#if (User.IsAuthenticated) {
<span>
Welcome
<strong>
#User.Identity.Name
</strong>
!
</span>
<span>[#Html.ActionLink("SignOut", "Login")]</span>
}
else {
<span>[#Html.ActionLink("SignIn", "Login")]</span>
}
which you would include in your layout:
#Html.Partial("_LogOnUserControl")

Allow "invalid" html markup in partial view in MVC 3

I have a problem with generating html with razor engine. In my case I have a app where a stored procedure lists a nested tree and have calculated how many submenus, how many siblings etc there are. And I need to have some logic in my partial view. And razor engine doesn't seem to like it since it seems to be invalid markup. How can I fix this to it prints out what I want?
<ul class="menu">
#foreach (var item in Model.NestedMenus)
{
if (item.StartNode > 0)
{
if (item.SubMenus > 0)
{
<li style="submenu">
}
else
{
<li style="menu">
}
#item.MenuName
}
else
{
</li>
}
}
</ul>
Must I use some old school Response.Write or summet? :)
/L
You need to prefix the lines with #: to prevent Razor from trying to parse the markup.
Otherwise, it will need to parse the markup in order to end the code block outside the top layer of markup.

Resources