MVC 3 - Binding to a Complex Type with a List type property

前端 未结 1 1993
时光取名叫无心
时光取名叫无心 2021-02-06 13:49

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()
    {
            


        
1条回答
  •  悲&欢浪女
    2021-02-06 14:22

    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.

    0 讨论(0)
提交回复
热议问题