如何使httppost actionresult仅绑定到viewModel的一部分

时间:2017-01-28 11:21:26

标签: asp.net-mvc-4

我有以下sudo-code

class viewModel
{
    public ICollection<modelA> parentModel
    public modelC formModel
}

class modelA
{
    public int ID {get;set;}
    public virtual Icollection<modelB> {get;set;}
}

class modelB
{
    public int ID {get;set;}
    public string SomeString {get;set;}
    public virtual modelA ModelA {get;set;}
}

class modelC
{
    public int ModelAID {get;set;}
    public string SomeString {get;set;}
}

因此。视图模型包含As的集合。每个A包含一个B集合,并且有一个单独的模型作为表单发回:表单将在页面上重复,在A的每个实例中一次,其中A.ID作为隐藏字段传递给ModelAID。页面上只允许一个表单发布,表单字段的id是formModel.ModelAID和.formModel.SomeString,因为它们是从viewModel的非父元素派生的。

如何让ActionResult仅绑定到formModel?

 [HttpPost]
 Public ActionResult Input(formModel vm)
 {
    ... by default the view model being passed back is full VM, I only want the formModel so the post signature does not match
 }

1 个答案:

答案 0 :(得分:1)

您可以尝试类似

的内容
public ActionResult Input([Bind(Prefix = "formModel ")]modelC model)
{

}
相关问题