我有以下型号:
class A
{
// ...some properties
public B InnerField { get; set; }
}
和
class B
{
public int Id { get; set; }
// ..other properties
}
和一个模型类A的页面在页面内部我有一个局部视图绑定到表单内的B类。 Id(在局部视图中)的值正确设置为模型的Id值(不同于0),但是当我提交页面时,模型的Id值为0. Id值不会在组件或其他地方修改。
页
...other parts of main page
<%using (Html.BeginForm("ModifyHotel", "Hotel",
FormMethod.Post, new { enctype = "multipart/form-data"}))
{%>
<% Html.RenderPartial("~/Views/Shared/ModifyBaseItem.ascx",
new ModifyItemRequestBaseView() { ItemId = Model.Item.Id });%>
<%}%>
...other parts of main page
部分视图
...other parts of partial view
<br/>
Add Photo: <%:Html.FileBoxFor(x => x.PhotoFile, null)%>
<br/>
Add Video: <%:Html.FileBoxFor(x => x.VideoFile, null)%>
<br/>
<input type="submit" value="Submit changes" />
...other parts of partial view
在发布帖子时,我该怎么做才能保持内模的价值?
谢谢,
答案 0 :(得分:2)
<强>控制器:强>
public class HomeController : Controller
{
public ActionResult Index()
{
A model = new A() { InnerField = new B() { Id = 5 }};
return View(model);
}
[HttpPost]
public ActionResult Index(B model)
{
//on postback the model should have the value 5 here
return View();
}
}
查看:强>
@model MvcApplication11.Models.A
@using (Html.BeginForm())
{
@Html.Partial("_IndexForm", Model.InnerField)
<input type="submit" />
}
<强>部分:强>
@model MvcApplication11.Models.B
@Html.EditorFor(m => m.Id)