Is there any good reason to use FormCollection instead of ViewModel?

Is there any good reason to use FormCollection instead of ViewModel? No. I have following issues. Issue – 1 In case FormCollection is being used…It will be mandatory to Type Cast the Primitive Type Values un-necessarily because while getting the entry of specific Index of the System.Collections.Specialized.NameValueCollection, value being returned is of type String. This … Read more

MVC Razor @foreach

What is the best practice on where the logic for the @foreach should be at? Nowhere, just get rid of it. You could use editor or display templates. So for example: @foreach (var item in Model.Foos) { <div>@item.Bar</div> } could perfectly fine be replaced by a display template: @Html.DisplayFor(x => x.Foos) and then you will … Read more

How to change ‘data-val-number’ message validation in MVC while it is generated by @Html helper

You can override the message by supplying the data-val-number attribute yourself when rendering the field. This overrides the default message. This works at least with MVC 4. @Html.EditorFor(model => model.MyNumberField, new { data_val_number=”Supply an integer, dude!” }) Remember that you have to use underscore in the attribute name for Razor to accept your attribute.

Difference Between $.getJSON() and $.ajax() in jQuery

Content-type You don’t need to specify that content-type on calls to MVC controller actions. The special “application/json; charset=utf-8” content-type is only necessary when calling ASP.NET AJAX “ScriptServices” and page methods. jQuery’s default contentType of “application/x-www-form-urlencoded” is appropriate for requesting an MVC controller action. More about that content-type here: JSON Hijacking and How ASP.NET AJAX 1.0 … Read more

How can I disable session state in ASP.NET MVC?

You could make your own ControllerFactory and DummyTempDataProvider. Something like this: public class NoSessionControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(Type controllerType) { var controller = base.GetControllerInstance(controllerType); ((Controller) controller).TempDataProvider = new DummyTempDataProvider(); return controller; } } public class DummyTempDataProvider : ITempDataProvider { public IDictionary<string, object> LoadTempData(ControllerContext controllerContext) { return new Dictionary<string, object>(); } public void … Read more