【问题标题】:ASP .NET MVC3 how to send data from partial ViewASP .NET MVC3 如何从部分视图发送数据
【发布时间】:2011-06-08 23:29:36
【问题描述】:

这里是场景: 我已经设置了几个 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 中传递所有内容。

更新: 特殊视图

@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>

【问题讨论】:

  • 我认为“无关字段”可能不是那么无关紧要......这些字段不是ThreadEditorPartialView 的一部分吗?您是否尝试过将它们从部分中删除以查看它是否有效?
  • 那些“无关字段”只是脚手架生成的,看起来像我发布的那些。我无法删除它们,因为我需要所有这些作为输入数据。

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


【解决方案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) 
{ 
    ...
}

【讨论】:

  • 工作。好的。不知道 EditorTemplates。
【解决方案2】:

_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) { }

【讨论】:

  • 这正是它们的输入方式。但这似乎不起作用。
  • @Łukasz - 你能发布至少两个部分之一的代码吗?
猜你喜欢
  • 2013-01-06
  • 2012-06-27
  • 2023-03-30
  • 2019-10-22
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多