Remote Validation for LIST of MODELs

You have not posted your code for the model or controller, but assuming you have a RemoteAttribute applied to property Username, for example public class MyModel { [Remote(“IsValidUserName”, “Person”)] public string Username { get; set; } } with a method in PersonController public JsonResult IsValidUserName(string Username) { …. } and the view @model List<Person> … … Read more

How does MVC 4 List Model Binding work?

There is a specific wire format for use with collections. This is discussed on Scott Hanselman’s blog here: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx Another blog entry from Phil Haack talks about this here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx Finally, a blog entry that does exactly what you want here: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

How to pass IEnumerable list to controller in MVC including checkbox state?

Use a list instead and replace your foreach loop with a for loop: @model IList<BlockedIPViewModel> @using (Html.BeginForm()) { @Html.AntiForgeryToken() @for (var i = 0; i < Model.Count; i++) { <tr> <td> @Html.HiddenFor(x => x[i].IP) @Html.CheckBoxFor(x => x[i].Checked) </td> <td> @Html.DisplayFor(x => x[i].IP) </td> </tr> } <div> <input type=”submit” value=”Unblock IPs” /> </div> } Alternatively you … Read more

ViewModel with List and editor templates

Assuming you have the following models: public abstract class TableInputModel { } public class RectangleTableInputModel : TableInputModel { public string Foo { get; set; } } public class CircleTableInputModel : TableInputModel { public string Bar { get; set; } } And the following controller: public class HomeController : Controller { public ActionResult Index() { var … Read more

ASP.NET MVC Binding to a dictionary

In ASP.NET MVC 4, the default model binder will bind dictionaries using the typical dictionary indexer syntax property[key]. If you have a Dictionary<string, string> in your model, you can now bind back to it directly with the following markup: <input type=”hidden” name=”MyDictionary[MyKey]” value=”MyValue” /> For example, if you want to use a set of checkboxes … Read more

A Partial View passing a collection using the Html.BeginCollectionItem helper

First start by creating a view model to represent what you want to edit. I’m assuming cashAmount is a monetary value, so therefore should be a decimal (add other validation and display attributes as required) public class CashRecipientVM { public int? ID { get; set; } public decimal Amount { get; set; } [Required(ErrorMessage = … Read more