MVC 5.1 Razor DisplayFor not working with Enum DisplayName

Create new folder Views/Shared/DisplayTemplates Add empty Partial View named Enum, to the folder Replace Enum View code with: @model Enum @if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata)) { // Display Enum using same names (from [Display] attributes) as in editors string displayName = null; foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model)) { if (item.Selected) { displayName = item.Text ?? item.Value; } … Read more

asp.net conditionally disable a tag helper (textarea)

It is actually very simple, the disable attribute is already working as you want – you can pass in a boolean value: <textarea asp-for=”Doc” disabled=”@Model.MustDisable”></textarea> if false the disabled attribute is not rendered: <textarea></textarea> if true the disabled attribute is set to “disabled”: <textarea disabled=”disabled”></textarea>

Html.Raw() in ASP.NET MVC Razor view

Html.Raw() returns IHtmlString, not the ordinary string. So, you cannot write them in opposite sides of : operator. Remove that .ToString() calling @{int count = 0;} @foreach (var item in Model.Resources) { @(count <= 3 ? Html.Raw(“<div class=\”resource-row\”>”): Html.Raw(“”)) // some code @(count <= 3 ? Html.Raw(“</div>”) : Html.Raw(“”)) @(count++) } By the way, returning … Read more

Better way to get active page link in MVC 3 Razor

A better approach is to use a HTML helper: using System.Web.Mvc; using System.Web.Mvc.Html; public static class MenuExtensions { public static MvcHtmlString MenuItem( this HtmlHelper htmlHelper, string text, string action, string controller ) { var li = new TagBuilder(“li”); var routeData = htmlHelper.ViewContext.RouteData; var currentAction = routeData.GetRequiredString(“action”); var currentController = routeData.GetRequiredString(“controller”); if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) && … Read more

Why doesn’t Visual Studio code formatting work properly for Razor markup?

Be sure to set the editor to use space characters and not tabs. The editor seems to completely lose its mind when tabs are used. This is a shame because all those space characters end up in the actual HTML output, greatly increasing the data transfer size. What I do is manually supplement the automatic … Read more

Does Razor syntax provide a compelling advantage in UI markup?

[Disclaimer: I’m one of the Microsoft developers on MVC and Razor, so I might be a bit biased :)] We designed Razor to be a concise templating language that uses only the minimal necessary amount of control characters. I would say that large parts of your views can be expressed with fewer characters than the … Read more