2

我有以下设置

public class Profile
{
    //Some Properties

    //Some Methods
}

/*Model Class*/
public class LineItem
{
    public Profile Profile {get;set;}
}

在我的控制器中。我有以下两个动作

public ActionResult GetRequest(){

    LineItem model = new LineItem();
    model.Profile = new Profile(){/*Initialize Properties*/};
    return View(model);
}

public ActionResult PostRequest(LineItem item(){
      item.Profile ....  /*Profile Is Null*/
      return View(...);
}

我已验证 LineItem 模型类上的 Profile 属性已在 GetRequest 上设置,并返回到视图,但我想知道在提交该模型时如何保留该复杂属性。我知道人们通常使用隐藏字段进行模型持久性,并且会话也可用,但似乎我有时应该能够告诉 ModelBinder(可能通过属性或注释)在我的模型上保留复杂的属性。

如何才能做到这一点?

4

1 回答 1

3

您可以@Html.HiddenFor(model => model.Profile.Id) @Html.HiddenFor(model => model.Profile.Name)对需要保留的 Model.Profile 的每个属性使用 etc.。您不应该触摸模型绑定器 - 它将使用标准模型绑定器自动绑定。如果您使用其他 html 帮助程序,它会为某些属性生成输入标签 - 您不应该使用@Html.HiddenFor()此属性。

于 2012-06-06T18:22:02.767 回答