开发者

ASP.NET MVC Subform data not posting

This seems simple enough but I am missing something:

Model:

public class MainModel
{

   public SubModel oSubmodel = new Submodel();

....

}

View:

@model myApp.Models.MainModel
@using (Html.BeginForm("Index","Account", FormMethod.Post, new { id = "form"}) 
{

     @Html.LabelFor(m => m.oSubmodel.prop1

     @Html.TextBoxFor(m => m.oSubmodel.prop1

}

Controller:

 [HttpPost]
 public ActionResult Index(MainModel oModel)
 {
      ....
      string prop = oModel.prop <-----------ok
      string prop开发者_Go百科1 = oModel.oSubmodel.prop1   <----------null
  }

The m.oSubmodel.prop1 data is display correctly in the view. When the data is posted back to the controller MainModel values are passed correctly, however - all submodel values are null.

Anybody give any insight?


Right OK. My Bad. The subModel need to be exposed as property off the main model for binding to work correctly on post:

So

public class MainModel
{

   public SubModel oSubmodel = new Submodel();

....

}

becomes:

public class MainModel
{

   public SubModel oSubmodel { get; set; }

....

}

Binding then works great. Thanks to those that responded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜