【问题标题】:Post a form with multiple partial views发布具有多个部分视图的表单
【发布时间】:2012-05-23 10:31:54
【问题描述】:

我目前正在尝试发布一个由两个强类型视图组成的表单。这个问题类似,但没有答案:

MVC 3 Razor Form Post w/ Multiple Strongly Typed Partial Views Not Binding

当我提交表单时,提交给控制器的模型始终为空。我花了几个小时试图让它工作。这似乎应该很简单。我在这里错过了什么吗?我不需要做 ajax 只需要能够发布到控制器并呈现一个新页面。

谢谢

这是我的视图代码:

<div>
    @using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"}))
    {
        ViewContext.FormContext.ValidationSummaryId = "valSumId";
        @Html.ValidationSummary(false, "Please fix these error(s) and try again.", new Dictionary<string, object> { { "id", "valSumId" } });
        @Html.Partial("_ReportOptions", Model.ReportOptions);
        @Html.Partial("_TransactionSearchFields", new ViewDataDictionary(viewData) { Model = Model.SearchCriteria });
    }

这是控制器中的代码:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TransactionReport(TransactionReportRequest reportRequest)
{
    var reportInfo = new List<TransactionReportItem>();

    if (ModelState.IsValid)
    {
        var reportData = _reportDataService.GetReportData(Search.MapToDomainSearchCriteria(reportRequest.SearchCriteria));
        if (reportData!=null)
        {
            reportInfo = reportData.ToList();
        }
        return View(reportInfo);
    }
    return View(reportInfo);
}

部分视图本身是无关紧要的,因为他们所做的只是竞标和展示他们的模型。

【问题讨论】:

  • 您的提交按钮在哪里?它需要在 Form 标签内。不确定你是没有包含它还是它在标签之外

标签: asp.net-mvc asp.net-mvc-3 razor http-post partial-views


【解决方案1】:

部分不是这里的方式。您正在寻找 EditorTemplates,这些都是为您想要的。在这种情况下,您的属性将很好地绑定到您的模型(您将提交)。

您的主视图将具有此表单(请注意,您只需使用EditorFor 而不是Partial;在这种情况下,您可能需要将该 viewData 参数放在 ViewBag 左右):

@using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"}))
{
    ViewContext.FormContext.ValidationSummaryId = "valSumId";
    @Html.ValidationSummary(false, "Please fix these error(s) and try again.", new Dictionary<string, object> { { "id", "valSumId" } });
    @Html.EditorFor(model => model.ReportOptions);
    @Html.EditorFor(model = Model.SearchCriteria });
}

现在您只需将您的部分拖到文件夹 ~/Shared/EditorTemplates/ 并重命名它们以匹配它们作为编辑器模板的模型名称。

~/Shared/EditorTemplates/ 文件夹中,创建一个新的“视图”,例如“SearchCriteria.cshtml”。在里面,将您要为其创建编辑器模板的类的类型作为“模型”。示例(示例类具有属性NameOtherCriteria):

@model MyNamespace.SearchCriteria
<ul>
    <!-- Note that I also use EditorFor for the properties; this way you can "nest" editor templates or create custom editor templates for system types (like DateTime or String or ...). -->
    <li>@Html.LabelFor(m => m.Name): @Html.EditorFor(m => m.Name)</li>
    <li>@Html.LabelFor(m => OtherCriteria): @Html.EditorFor(m => m.OtherCriteria</li>
</ul>

一些关于它们的好读物:

【讨论】:

  • 是的,这是一个更好的解决方案。 Grails 有类似的东西,我应该使用 grails 术语搜索(我的意思是 binged)它:)这很好用!感谢您的帮助!
  • 有时,当您不知道某项技术的正确术语时,很难找到一些东西。
  • 由于链接失效,这里有一篇关于EditorTemplates的不错教程
  • @bnu :感谢您的通知。我用 2 个不错的替代品替换了旧链接,并添加了一些小示例代码。
【解决方案2】:

您应该为 PartialView 的字段添加前缀。这将使绑定数据正确。

所以改为:

@Html.Partial("_ReportOptions", Model.ReportOptions);

用途:

@Html.Partial("_ReportOptions", Model.ReportOptions, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "ReportOptions" }})

【讨论】:

    【解决方案3】:

    我同意@Styxxy 和@Tony,编辑器模板是更好的解决方案。但是,您的问题是您将子模型提供给部分视图。因此,当局部视图呈现时,它不知道它是更大模型的一部分,也不会生成正确的名称属性。

    如果您坚持使用部分而不是编辑器模板,那么我建议只将模型传递给部分,然后让每个部分执行 Model.Whatever.Foo,它将生成正确的名称属性以进行绑定。

    【讨论】:

      【解决方案4】:

      尝试使用 EditorTemplates 而不是 Partials http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/

      【讨论】:

        【解决方案5】:
        @Html.Partial("_ReportOptions", Model.Contact, new ViewDataDictionary()
                   {
                       TemplateInfo = new TemplateInfo()
                           {
                               HtmlFieldPrefix = "Contact"
                           }
                   })
        
        )
        
        
        @Html.Partial("_TransactionSearchFields", Model.SearchCriteria, new  
         ViewDataDictionary()
                   {
                       TemplateInfo = new TemplateInfo()
                           {
                               HtmlFieldPrefix = "SearchCriteria"
                           }
                   })
        

        【讨论】:

          猜你喜欢
          • 2018-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多