MVC3 - 回传距离的RenderPartial模型 [英] MVC3 - Pass back a model from RenderPartial

查看:66
本文介绍了MVC3 - 回传距离的RenderPartial模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC3一个网页的pageModel的典范。

I have a page in MVC3 with a model of "pageModel".

在这个页面我有:

@{ Html.RenderPartial("_subPage", Model.subModel); } (Pagemodel.submodel)

在我的控制器我做的:

 [Authorize]
 [AcceptVerbs(HttpVerbs.Post)]
 public ActionResult Results(pagemodel model, string frmAction)
 {
 }

该网页加载罚款的第一次,但是当我回发到 httpPost 操作, model.submodel 是始终为空。

我的问题是,我该如何返回从的RenderPartial一个更新的模式(如果有的话)。我可以让我的模型转化为局部的,但不是回来了!

My question is, how do I return an updated model from the RenderPartial (if at all). I can get my model INTO the partial, but not back!

推荐答案

有谐音的问题是,他们没有preserve导航上下文。这意味着,你可能已经把这里面的任何部分输入字段将有不正确的名称,默认模式粘结剂将无法当你POST检索值返回。你的HTML看起来像这样:

The problem with partials is that they do not preserve the navigational context. This means that any input fields that you might have put inside this partial will have incorrect names and the default model binder will not be able to retrieve the values back when you POST. Your HTML will look like this:

<input type="text" name="Prop1" value="property 1 value" />
<input type="text" name="Prop2" value="property 2 value" />

而正确的是:

<input type="text" name="subModel.Prop1" value="property 1 value" />
<input type="text" name="subModel.Prop2" value="property 2 value" />

为了实现这一正确的标记,我会建议你使用编辑器的模板。

In order to achieve this correct markup I would recommend you using editor templates.

所以你替换:

@{ Html.RenderPartial("_subPage", Model.subModel); }

@Html.EditorFor(x => x.subModel)

然后您移动 _subPage.cshtml 部分为〜/查看/共享/ EditorTemplates / SubModelType.cshtml 其中, SubModelType 子模型属性的类型:

and then you move your _subPage.cshtml partial into ~/Views/Shared/EditorTemplates/SubModelType.cshtml where SubModelType is the type of the subModel property:

@model SubModelType
@Html.EditorFor(x => x.Prop1)
@Html.EditorFor(x => x.Prop2)

现在,当你在生成的HTML对应的输入字段名称应与子模型 pfixed $ P $和POST控制器动作中的 model.subModel 属性将这段时间进行正确的初始化并填充从输入字段是由用户输入的值。

Now when you look at the generated HTML the corresponding input field names should be prefixed with subModel and inside the POST controller action the model.subModel property will this time be properly initialized and populated from the values that were entered by the user in the input fields.

这篇关于MVC3 - 回传距离的RenderPartial模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆