twitter-bootstrap 在表单中的 Bootstrap 模式中提交表单...?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16302198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:01:38  来源:igfitidea点击:

Submit form within Bootstrap modal within Form...?

formsasp.net-mvc-4twitter-bootstrapmodal-dialog

提问by Chris Paton

I have a form that allows users to log details. One element of that form is to select a particular user. If that user is not there, I want to be able to create a modal to create a new user, submit that new user to the database and then have that user available in the drop down list for all users. I'm using MVC 4 and Twitter Bootstrap framework. Any suggestions on how to do this?

我有一个允许用户记录详细信息的表单。该表单的一个要素是选择特定用户。如果该用户不存在,我希望能够创建一个模式来创建一个新用户,将该新用户提交到数据库,然后让该用户在所有用户的下拉列表中可用。我正在使用 MVC 4 和 Twitter Bootstrap 框架。关于如何做到这一点的任何建议?

I'm able to create the layout and it all displays fine, but the main issue at the moment is that when I submit the form inside the modal it doesn't pass the model data through to the controller on submission. Is that due to the fact that you can't have forms within forms? Shouldn't I have had other issues if that's the case?

我能够创建布局并且一切都显示正常,但目前的主要问题是,当我在模态内提交表单时,它不会在提交时将模型数据传递给控制器​​。这是因为您不能在表格中使用表格吗?如果是这样的话,我不应该有其他问题吗?

Any help or guidance would be greatly appreciated. Thanks!

任何帮助或指导将不胜感激。谢谢!

回答by MattSull

Have a look at this answer where I show how to use a Bootstrap modal with a form, there should be enough in it to get you on track:

看看这个答案,我展示了如何使用带有表单的 Bootstrap 模式,其中应该有足够的内容让您走上正轨:

MVC 4 Edit modal form using Bootstrap

MVC 4 使用 Bootstrap 编辑模态表单

回答by Chris Paton

I actually solved the issue by putting the second form (for the modal) outside the main form and created a partial view to create the modal div (and drop down list to be updated by the modal) back inside the main form. That way, when the code renders in html for there are two distinctly separate forms. When the second form (which is inside the modal) submits it's data I get it to redirect to the main view. That way, the information that the modal adds to the main view is then submitted and available for use.

我实际上通过将第二个表单(用于模态)放在主表单之外并创建了一个局部视图来创建模态 div(以及由模态更新的下拉列表)回到主表单中来解决这个问题。这样,当代码在 html 中呈现时,有两种截然不同的形式。当第二个表单(在模态内)提交它的数据时,我让它重定向到主视图。这样,模态添加到主视图的信息就会被提交并可供使用。

I hope this makes sense to people. I'm pretty new to MVC so am hacking away blindly!

我希望这对人们有意义。我对 MVC 还很陌生,所以我盲目地砍掉了!

回答by Hezy Ziv

The problem is that you wrap the modal with the form, that is the reason why it does not submit.

问题是你用表单包装了模态,这就是它不提交的原因。

You should put the form in the modal body, it will work.

您应该将表单放在模态正文中,它会起作用。