Routing with and without controller name in ASP.NET MVC 4

This should do it: routes.MapRoute( name: “About”, url: “About”, defaults: new { controller = “Public”, action = “About” } ); routes.MapRoute( name: “MyPageSummary”, url: “MyPage”, defaults: new { controller = “MyPage”, action = “Summary” } ); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Public”, action = “Start”, id = UrlParameter.Optional } );

There is no ViewData item of type ‘IEnumerable’ that has the key country

If you were using DropDownListFor like this: @Html.DropDownListFor(m => m.SelectedItemId, Model.MySelectList) where MySelectList in the model was a property of type SelectList, this error could be thrown if the property was null. Avoid this by simply initializing it in constructor, like this: public MyModel() { MySelectList = new SelectList(new List<string>()); // empty list of anything… … Read more

LINQ to Entities does not recognize the method ‘System.String ToString()’ method in MVC 4

You got this error because Entity Framework does not know how to execute .ToString() method in sql. So you should load the data by using ToList and then translate into SelectListItem as: var query = dba.blob.ToList().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name_company, Selected = c.id.Equals(3) }); Edit: To make it more … Read more

Enum localization

You can implement a description attribute. public class LocalizedDescriptionAttribute : DescriptionAttribute { private readonly string _resourceKey; private readonly ResourceManager _resource; public LocalizedDescriptionAttribute(string resourceKey, Type resourceType) { _resource = new ResourceManager(resourceType); _resourceKey = resourceKey; } public override string Description { get { string displayName = _resource.GetString(_resourceKey); return string.IsNullOrEmpty(displayName) ? string.Format(“[[{0}]]”, _resourceKey) : displayName; } } } … Read more

ASP MVC Razor encode special characters in input placeholder

I think this post will help you: HTML encode decode c# MVC4 I think there are other ways to get this behaviour, but this is one option of using the TextBox: @Html.TextBox(“CompanyName”, HttpUtility.HtmlEncode(“Your company&#39;s name”)) There is also HttpUtility.HtmlDecode, which might help with our save action. update if you wrap HttpUtility.HtmlDecode around your place holder: … Read more

show only the date in @Html.EditorFor helper

You need to use the ISO format when using type=”date” [DataType(DataType.Date, ErrorMessage=”Date only”)] [DisplayFormat(DataFormatString = “{0:yyyy-MM-dd}”, ApplyFormatInEditMode = true)] public DateTime? YearBought { get; set; } This will display the date in the browsers culture. Note there is no need to add @type = “date”. The EditorFor() method will add that because of the DataType … Read more