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

Html.EnumDropdownListFor: Showing a default text

Try to change the Index of LicenseTypes start from 1 not 0 like below: public enum LicenseTypes { Trial = 1, Paid = 2 } Then you can use Range attribute to validate the selected license type like below: public class YourViewModel { //Other properties [Range(1,int.MaxValue,ErrorMessage = “Select a correct license”)] public LicenseTypes LicenseTypes { … Read more

ASP.NET MVC 5 error handling

The best way is using Global.Asax, because you can manage all types of errors (Ajax calls/ all of unexpected Errors). with others you can’t do it. Like this: protected void Application_Error() { HttpContext httpContext = HttpContext.Current; if (httpContext != null) { RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext; /* When the request is ajax the system can automatically … Read more

How can I fix assembly version conflicts with JSON.NET after updating NuGet package references in a new ASP.NET MVC 5 project?

Here the steps I used to fix the warning: Unload project in VS Edit .csproj file Search for all references to Newtonsoft.Json assembly Found two, one to v6 and one to v5 Replace the reference to v5 with v6 Reload project Build and notice assembly reference failure View References and see that there are now … Read more