简体   繁体   English

MVC4使用一个提交按钮以多种形式编辑复杂对象

[英]MVC4 edit a complex object in multiple forms with a single submit button

There is a huge object which I need to edit in different tabs. 有一个巨大的对象,我需要在不同的选项卡中进行编辑。 Each tab is a partial view with a single form inside. 每个选项卡都是局部视图,内部有一个表单。 Is it possible to submit data from all the partial forms with a single submit button? 是否可以通过一个提交按钮从所有部分表单提交数据? I would like to have combined model object in my POST action method to save it further. 我想在我的POST操作方法中合并模型对象,以进一步保存它。

In your page, just make sure that all your partial views are wrapped by the main form: 在您的页面中,只需确保您的所有局部视图都被主表单包裹:

@model MyNamespace.BigModel;

@using (Html.BeginForm())
{
    <!-- Other tab code would go around here -->
    @Html.Partial("Partial1", BigModel)
    @Html.Partial("Partial2", BigModel)
    @Html.Partial("Partial3", BigModel)
    <!-- Other tab code would go around here -->
}

Then a submit button anywhere within the form would submit all the data. 然后,表单内任何位置的提交按钮将提交所有数据。

You could use something on the client side to do this (for example using jQuery): 您可以在客户端使用某些方法(例如,使用jQuery):

$('#button-to-submit-everything').click(function () {
    $('form').submit();
});

This would submit an individual post to each partial view. 这将向每个局部视图提交一个单独的帖子。

If you only want one post then I believe you would need to have only one form encompassing all of your partial views. 如果您只想发表一篇文章,那么我相信您只需要一种表格即可涵盖您的所有部分观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM