ASP .NET MVC3如何从部分View发送数据

时间:2011-06-08 13:08:14

标签: asp.net-mvc asp.net-mvc-3

这里是场景: 我设置了几个ViewModel,如:

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>

2 个答案:

答案 0 :(得分:1)

我建议您使用编辑器模板而不是部分模板,因为它们将负责为输入字段生成专有名称,以便模型绑定器能够正确解析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) 
{ 
    ...
}

答案 1 :(得分:0)

_ThreadEditor_SpecialProperties看起来像什么?

如果部分分别键入ThreadEditorPartialViewThreadEditorSpecial,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) { }