【发布时间】:2012-02-16 14:47:45
【问题描述】:
我有两个看起来像这样的 MVC 模型:
public class OtherModel
{
[Required]
[Display(Name = "Another ID")]
public int id{ get; set; }
}
public class MyModel
{
[Required]
[Display(Name = "ID")]
public int id { get; set; }
public PlayerModel otherModel = new OtherModel ();
}
我的控制器有一个名为 USE 的 [HttpPost] 操作,如下所示:
[HttpPost]
public ActionResult Use(MyModel myModel)
{
/// myModel.otherModel.id is 0 here!!
}
此操作采用 MyModel。当我的表单被发布时, otherModel 变量包含一个 0 作为 id 值。现在,包含表单的视图被传递一个 MyModel 并实际在页面上显示 otherModel.id。问题是发布操作不是 将表单数据正确编组到 otherModel 对象中,我不知道为什么。
另一个注意事项:当我检查帖子的表单数据标题时,我清楚地看到 otherModel.id 具有我期望的值。
为什么这些数据在我的 otherModel 对象中没有正确显示?
提前谢谢你!
【问题讨论】:
-
您可以发布进入控制器的表单标题吗?
-
这也让我想知道你是否设置了 id 和另一个 id?
-
otherModel 真的是公共成员而不是属性吗?
-
@Jesse - 我现在没有确切的标题,事实上,与实际情况相比,上面的这些模型被简化了,但这也不起作用。标题肯定包含:“otherModel.id”和正确的值。事实上,我现在正在做的是解决这个问题:if(!int.TryParse(this.Request.Form["otherModel.id"], out value))。
-
我认为你应该展示你的 Razor 代码,可能是你没有通过 Action 发送模型
标签: c# .net asp.net-mvc-3 forms post