发布具有多个局部视图的表单 [英] Post a form with multiple partial views

查看:34
本文介绍了发布具有多个局部视图的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

MVC 3 Razor带有多个强类型部分视图的表单帖子不具有约束力

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

谢谢

这是我的视图代码:

@using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"})){ViewContext.FormContext.ValidationSummaryId = "valSumId";@Html.ValidationSummary(false, "请修复这些错误并重试.", new Dictionary { { "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();如果(模型状态.IsValid){var reportData = _reportDataService.GetReportData(Search.MapToDomainSearchCriteria(reportRequest.SearchCriteria));如果(报告数据!=空){reportInfo = reportData.ToList();}返回视图(报告信息);}返回视图(报告信息);}

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

解决方案

Partials 不是这里的方法.您正在寻找 EditorTemplates,它们是为您的需要而制作的.在这种情况下,您的属性将很好地绑定到您的模型(您将提交).

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

@using (Html.BeginForm("TransactionReport", "Reports", FormMethod.Post, new {id="report_request"})){ViewContext.FormContext.ValidationSummaryId = "valSumId";@Html.ValidationSummary(false, "请修复这些错误并重试.", new Dictionary { { "id", "valSumId" } });@Html.EditorFor(model => model.ReportOptions);@Html.EditorFor(model = Model.SearchCriteria });}

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

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

@model MyNamespace.SearchCriteria<ul><!-- 请注意,我还使用 EditorFor 作为属性;通过这种方式,您可以嵌套"编辑器模板或为系统类型(如 DateTime 或 String 或 ...)创建自定义编辑器模板.-->
  • @Html.LabelFor(m => m.Name):@Html.EditorFor(m => m.Name)
  • <li>@Html.LabelFor(m => OtherCriteria):@Html.EditorFor(m => m.OtherCriteria</li>

    一些关于它们的好读物:

    I'm currently trying to post a form composed of two strongly typed views. This question is similar but it doesn't have an answer:

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

    When I submit form the model submitted to the controller is always null. I've spent a couple of hours trying to get this to work. This seems like it should be simple. Am I missing something here? I don't need to do ajax just need to be able to post to the controller and render a new page.

    Thanks

    Here's my view code:

    <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 });
        }
    

    Here's the code in the controller:

    [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);
    }
    

    The partial views themselves are pretty irrelevant since all they are doing is biding and displaying their models.

    解决方案

    Partials are not the way to go here. You are looking for EditorTemplates, these are made for what you want. This case, your properties will be nicely bound to your model (that you will submit).

    Your main View will have this form (note that you only have to use EditorFor instead of Partial; in this case, you probably will need to put that viewData parameter in the ViewBag or so):

    @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 });
    }
    

    Now you only have to drag your partials to the folder ~/Shared/EditorTemplates/ and rename them to match the model name they are the editor templates for.

    In the ~/Shared/EditorTemplates/ folder, make a new "view", example "SearchCriteria.cshtml". Inside, put as "model" the type of class you which to create an editor template for. Example (example class has properties Name and OtherCriteria):

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

    Some good reading about them:

    这篇关于发布具有多个局部视图的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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