下面是一个场景:我设置了几个ViewModels,如下所示:
public class ThreadEditorView
{
public int ForumID { get; set; }
public ThreadEditorPartialView ThreadEditor { get; set; }
public ThreadEditorSpecial ThreadSpecial { get; set; }
}现在,我拥有和视图:
@using (Html.BeginForm("NewThread", "Thread", FormMethod.Post,
new {@enctype="multipart/form-data"})) {
@Html.ValidationSummary(true)
<fieldset>
@Html.Partial("_ThreadEditor", Model.ThreadEditor)
@Html.Partial("_SpecialProperties", Model.ThreadSpecial)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}问题是如何将数据从部分视图传递到控制器?我知道我可以简单地这样做:
public ActionResult NewThread(ThreadEditorView modelEditor,
ThreadEditorPartialView blabla, ThreadEditorSpecial zumg)但是这看起来并不是很方便,我想在ThreadEditorView中传递所有的东西。
更新: SpecialView
@model Vaniv.Core.ViewModel.Forums.ThreadEditorSpecial
<div class="editor-label">
@Html.LabelFor(model => model.IsSticky)
</div>
<div class="editor-field">
@Html.RadioButtonFor(model => model.IsSticky, false)
@Html.ValidationMessageFor(model => model.IsSticky)
</div>
(some irrevalnt forms)
<div class="editor-field">
@Html.EditorFor(model => model.IsLocked)
@Html.ValidationMessageFor(model => model.IsLocked)
</div>和编辑:
@model Vaniv.Core.ViewModel.Forums.ThreadEditorPartialView
<legend>ThreadEditorPartialView</legend>
@Html.HiddenFor(model => model.ForumID)
<div class="editor-label">
@Html.LabelFor(model => model.ThreadName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ThreadName)
@Html.ValidationMessageFor(model => model.ThreadName)
</div>(有些表单是无关紧要的)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>发布于 2011-06-08 23:29:37
我建议您使用编辑器模板而不是部分参数,因为它们将负责为输入字段生成适当的名称,以便模型绑定器能够正确地解析POST操作中的值:
@using (Html.BeginForm("NewThread", "Thread", FormMethod.Post,
new {@enctype="multipart/form-data"})) {
@Html.ValidationSummary(true)
<fieldset>
@Html.EditorFor(x => x.ThreadEditor)
@Html.EditorFor(x => x.ThreadSpecial)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}并拥有相应的编辑器模板(请注意编辑器模板的名称和位置很重要):
~/Views/Shared/EditorTemplates/ThreadEditorPartialView.cshtml
@model ThreadEditorPartialView
<legend>ThreadEditorPartialView</legend>
@Html.HiddenFor(model => model.ForumID)
<div class="editor-label">
@Html.LabelFor(model => model.ThreadName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ThreadName)
@Html.ValidationMessageFor(model => model.ThreadName)
</div>和~/Views/Shared/EditorTemplates/ThreadEditorSpecial.cshtml
@model ThreadEditorSpecial
...现在,您的控制器操作可以简单地如下所示:
public ActionResult NewThread(ThreadEditorView modelEditor)
{
...
}发布于 2011-06-08 21:13:58
_ThreadEditor和_SpecialProperties是什么样子的?
如果将部分参数分别类型化为ThreadEditorPartialView和ThreadEditorSpecial,ASP.NET MVC将呈现带有有助于其在POST上适当绑定模型的名称的文本框
_ThreadEditor:
@model ThreadEditorPartialView
// rest of the view here_SpecialProperties:
@model ThreadEditorSpecial
// rest of the view here使用正确类型的部分参数,您的操作只需接受一个参数:
public ActionResult NewThread(ThreadEditorView modelEditor) { }https://stackoverflow.com/questions/6279175
复制相似问题