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
data-binding asp.net-mvc asp.net-mvc-3
user338195 Aug 24 '11 at 1:59 a.m. 2011-08-24 13:59
source share