MVC 3 - 绑定到一个复杂类型列表类型属性 [英] MVC 3 - Binding to a Complex Type with a List type property
问题描述
我有以下视图模型,它会通过我的工作搜索控制中。
I have a following view model and it will be used by a search control that 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, SearchCategoriesViewModel has the following structure:
public class SearchCategoriesViewModel
{
[Display(Name = "Categories")]
public IList<SearchCategoryViewModel> Categories { get; set; }
public SearchCategoriesViewModel()
{
Categories = new List<SearchCategoryViewModel>();
}
}
最后,搜索类别视图模型的结构如下:
And, finally, 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; }
}
当我提交一个搜索请求,SearchLocationViewModel自带通过与提交的参数,但是,SearchCategoriesViewModel来通过空(NOT NULL)。
When I submit a search request, the SearchLocationViewModel comes through with submitted parameters, however, SearchCategoriesViewModel comes through empty (not null).
下面是我的SearchCategoryViewModel编辑模板:
Below 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 generate the 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 end up with 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>
我怀疑是参数没有未来通过,因为生成的标记是错误的。难道你们中的任何尝试从复杂的对象产生局部的看法?我并不想通过IEnumerable的,我宁愿在一个单独的封装类,这样我可以扩展/如果在未来需要将其删除。
I suspect that the parameters are not coming through because generated markup is wrong. Did any of you try generating partial views from complex objects? I don't want to pass IEnumerable, I would rather have it encapsulated in a seperate class so that I can extend/remove it in the future if needed.
感谢您
推荐答案
由于你有一个静态列表,您可以快速破解你的方式来创建标记将被正确绑定:
Because you have a static list, you can quickly hack your way to creating markup which will be bound correctly:
@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 suggest that you reconsider how you are generating the markup in your partial view.
这篇关于MVC 3 - 绑定到一个复杂类型列表类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!