MVC-模型绑定的复杂类型 [英] MVC- Model Binding on a Complex Type

查看:107
本文介绍了MVC-模型绑定的复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的型号:

public class Search
{
    public int Id { get; set; }
    public string UserDefinedName { get; set; }
    public SearchAge Age { get; set; }
    public virtual ICollection<SearchBachelors> Bachelors { get; set; }
}

和我渲染年龄财产局部视图动态使用是这样的:

And I render the partial view for Age property dynamically using something like this:

@Ajax.ActionLink("Age", "SearchAge", new AjaxOptions { UpdateTargetId = "search-stage", InsertionMode = InsertionMode.InsertAfter })

这部分观点如下:

@model HireWireWeb.Models.Campaign.Search.Search
<div>
    @Html.DropDownListFor(m => m.Age.MaximumAge, HireWireWeb.Models.Constants.SLIST_AGE_LIST)
    @Html.DropDownListFor(m => m.Age.MinimumAge, HireWireWeb.Models.Constants.SLIST_AGE_LIST)
</div>

(请注意,我不是过客SearchAge的模式在这里,而是它的父,搜索,因为这只是我怎样才能张贴在形式SearchAge的价值观提交下所呈现的搜索表) - 按照这个答案

我的问题是,我该怎么办的局部视图同样的事情学士属性,因为它是一个ICollection的?

My question is, how can I do the same thing with the partial view for "Bachelors" property, since it's an ICollection?

推荐答案

MVC的标准模型,结合系统需要能够创造出被束缚的类型。它不能创建一个接口,所以要解决这个问题,最简单的方法是将类型更改为列表&LT; SearchBachelors&GT; 。然后,您可以有部分观点是这样的:

MVC's standard model-binding system needs to be able to create the Types being 'bound'. It can't create an interface, so the simplest way to fix this issue is to change the type to List<SearchBachelors>. You can then have a partial view something like this:

@model HireWireWeb.Models.Campaign.Search.Search
<div>
    @for(var i = 0;i<Model.Bachelors.Count;++i)
    {
        @Html.TextBoxFor(m => m.Bachelors[i].Name)
    }
</div>

...渲染输出会以这样的方式来约束到使用列表中的元素的索引你的列表属性来命名,当您提交形式。

...the rendered output will be named in such a way as to be bound into your List property using the indexes of the elements in the list when you submit the form.

这篇关于MVC-模型绑定的复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆