MVC 3 - binding to a complex type with a property of type List

I have the following presentation model and it will be used by the control I'm working on.

public class SearchViewModel { public SearchViewModel() { SearchLocation = new SearchLocationViewModel(); SearchCategories = new SearchCategoriesViewModel(); } public SearchLocationViewModel SearchLocation { get; set; } public SearchCategoriesViewModel SearchCategories { get; set; } } 

SearchCategoriesViewModel now has the following structure:

 public class SearchCategoriesViewModel { [Display(Name = "Categories")] public IList<SearchCategoryViewModel> Categories { get; set; } public SearchCategoriesViewModel() { Categories = new List<SearchCategoryViewModel>(); } } 

And finally, the search category view model has the following structure:

  public class SearchCategoryViewModel { [Required] [Display(Name="Id")] public int Id { get; set; } [Display(Name="Name")] public String Name { get; set; } public bool IsSelected { get; set; } } 

When I submit a search request, the SearchLocationViewModel comes with the parameters presented, however the SearchCategoriesViewModel comes in through the empty (not empty).

The following is an editor template for my SearchCategoryViewModel:

 @model MyDLL.WebUI.Models.SearchCategoriesViewModel @foreach (var c in Model.Categories) { @Html.Label(c.Name); @Html.CheckBox(c.Name,c.IsSelected); } 

I use the following view to create search controls:

 @using (Html.BeginForm()) { @Html.ValidationSummary(true) <div id="search"> @Html.EditorFor(m => m.SearchCategories, "SearchCategory") @Html.EditorFor(m => m.SearchLocation, "SearchLocation") </div> <p> <input type="submit" value="Create" /> </p> } 

I get the following markup:

  <h2>Search</h2> <form action="/Settings/Search" method="post"> <label for="SearchCategories_Professional">Professional</label> <input id="SearchCategories_Professional" name="SearchCategories.Professional" type="checkbox" value="true" /> <input name="SearchCategories.Professional" type="hidden" value="false" /> <label for="SearchCategories_Associate">Associate</label><input id="SearchCategories_Associate" name="SearchCategories.Associate" type="checkbox" value="true" /> <input name="SearchCategories.Associate" type="hidden" value="false" /> <p> <input type="submit" value="Create" /> </p> </form> 

I suspect that the parameters will not pass, because the generated markup is incorrect. Have any of you tried to generate partial views from complex objects? I do not want to pass IEnumerable, I would prefer that it be encapsulated in a separate class, so that I can expand / delete it in the future in the future.

thank

+5
data-binding asp.net-mvc asp.net-mvc-3
Aug 24 '11 at 1:59 a.m.
source share
1 answer

Since you have a static list, you can quickly crack your way to creating markup that will be correctly linked:

 @model MyDLL.WebUI.Models.SearchCategoriesViewModel @{ var i = 0; } @foreach (var c in Model.Categories) { @Html.Hidden("Categories[" + i.ToString() + "].Id", c.Id); @Html.Hidden("Categories[" + i.ToString() + "].Name", c.Name); @Html.Label(c.Name); @Html.CheckBox("Categories[" + i.ToString() + "].IsSelected",c.IsSelected); } 

This is a quick and ugly solution. However, I would advise you to reconsider how you create markup in its partial form.

+5
Aug 24 2018-11-21T00:
source share



All Articles