发布多个局部视图的形式 [英] Post a form with multiple partial views

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

问题描述

我目前正在尝试后一两强类型的意见组成形式。这个问题是类似的,但它不具有一个答案:

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:

<一个href=\"http://stackoverflow.com/questions/8592136/mvc-3-razor-form-post-w-multiple-strongly-typed-partial-views-not-binding\">MVC 3剃刀表单提交瓦特/多强类型的局部视图不具约束力

当我提交表单提交给控制器的型号总是空。我花了几个小时试图让这个工作。这似乎是它应该很简单。我失去了一些东西在这里?我并不需要做阿贾克斯只需要能够发布到控制器,并呈现了新的一页。

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.

感谢

下面是我的看法code:

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

下面是在控制器中的code:

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

的部分观点本身是pretty无关紧要,因为他们所做的一切是招标和显示他们的模型。

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

推荐答案

局部模板不走这里的路。您正在寻找EditorTemplates,这些都是你想要的东西的。这种情况下,你的属性将很好地绑定到模型(你会提交)。

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).

您主视图都会有这样的形式(请注意,你只需要使用 EditorFor 而不是部分;在这种情况下,你可能需要把在ViewBag或使可视数据参数):

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

现在,你只需要拖动你的谐音到文件夹〜/共享/ EditorTemplates / 将其重新命名,以配合他们的编辑模板的型号名称。

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.

另外,也要看看 这里 相当一个很好的介绍EditorTemplates。

Also look here for quite a good introduction to EditorTemplates.

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

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