0

我有一个包装另一个模型的模型包装类:

public class LogicPage {
    public MyPage Pg { get; set; }
}

public class MyPage {
     public string name { get; set; }
}

在我的创建表单视图中,我使用强类型表单助手:

@model MyAppCore.Components.LogicPage
@using (Html.BeginForm("Create","PageAdmin",FormMethod.Post)) {
     <div class="editor-field">
     @Html.EditorFor(model => model.Pg.name)
     @Html.ValidationMessageFor(model => model.Pg.name)
 </div>
 ...
}

在我的控制器中,我将操作定义为:

[HttpPost]
public ActionResult Create(LogicPage logicpg)
{
   return View("View",logicpg.Pg);
}

但是,当 MyPage Pg 显示在列出模型(MyPage)详细信息的“视图”中时,不会显示用户输入的数据,显示的页面详细信息都是 MyPage 默认值(使用 LogicPage 的默认无参数构造函数初始化)。看起来表单中的数据没有传递给动作中的模型。有人可以帮我为什么数据没有通过吗?

为了澄清更多,我有两个视图模型 LogicPage 的“创建”表单视图和模型 MyPage 的“视图”详细视图

创建.cshtml

@model MyAppCore.Components.LogicPage

查看.cshtml

@model MyAppCore.Components.MyPage

谢谢

4

1 回答 1

0

你说你在做

[HttpPost]
public ActionResult Create(LogicPage logicpg)
{
   return View("View",logicpg.Pg);
}

但鉴于 MyPage 不继承自 LogicPage,并且您的视图需要 LogicPage 的实例,我认为您应该这样做:

[HttpPost]
public ActionResult Create(LogicPage logicpg)
{
   return View("View",logicpg);
}
于 2012-05-25T04:20:30.987 回答