简体   繁体   English

在ASP.NET MVC中使用静态和动态控件提交表单

[英]submit form with static and dynaic controls in ASP.NET MVC

I have a web page with some static controls (defined in server) and dynamic controls, (created by JavaScript). 我有一个网页,其中包含一些静态控件(在服务器中定义)和动态控件(由JavaScript创建)。 How can I get the data from both static and dynamic controls in the controller on form post? 如何在表单发布时从控制器的静态和动态控件中获取数据?

All my search leads to use JSON with AJAX. 我所有的搜索结果都将JSON与AJAX结合使用。 But I am looking for how to pass JSON on form submit. 但是我正在寻找如何在表单提交上传递JSON。 I want to navigate to different page with relevant data after successful submit. 成功提交后,我想导航到具有相关数据的其他页面。 Is it possible to return to a different view (in the controller) when the page is posted through AJAX? 通过AJAX发布页面时是否可以返回到其他视图(在控制器中)? Is there any other options available? 还有其他选择吗?

I guess that the dynamic controls you are injecting into your form represent input elements that have specific names. 我猜想您要注入到表单中的动态控件代表具有特定名称的输入元素。 If they don't then you probably should consider changing your code so that the dynamic controls you are inserting have names. 如果没有,则您可能应该考虑更改代码,以便要插入的动态控件具有名称。 You might take a look at the following article for an example: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ 您可以查看以下文章作为示例: http : //blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

Once you have all the elements inside the form, submitting it is just a matter of the user clicking on the submit button. 一旦您将所有元素包含在表单中,提交就可以由用户单击“提交”按钮来解决。 Your controller action will receive a view model with all the properties. 您的控制器动作将收到具有所有属性的视图模型。 And if you want to use AJAX and still redirect, you could set the window.location.href property inside your AJAX success callback to the desired url you are willing to redirect. 而且,如果您想使用AJAX并仍然重定向,则可以将AJAX成功回调中的window.location.href属性设置为您希望重定向的URL。

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

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