When a submit button triggers a second form, the second form's submit button sends a POST request to the action of the original form

我与影子孤独终老i 提交于 2019-12-12 01:14:16

问题


I've been having trouble with MVC forms, specifically having a form submit and display a second form.

The second form's submit button goes to the action of the original form. I asked if forms could be nested: Why do nested forms in Asp.net mvc not have scope?. Since form tags cannot indeed be nested, I changed things so the forms are in separate divs thinking that woudl resolve the problem.

After the user clicks submit on the first form, the second form appears. When the user clicks on that form's submit button, it POSTs to the controller action of the original form, not the one named after the view and viewmodel of the new form.

The form being inserted is using Ajax.BeginForm(new AjaxOptions {UpdateTargetId="X"}))

The second form has an <input type="submit"> tag at its end. Again, when the user clicks this button, none of the values in the form's model get sent back to the controller.

I've tried explicitly specifying the controller action in the Ajax link:

<%: Ajax.ActionLink("Add OS","OS",new AjaxOption{UpdateTargetId="", HttpMethod="POST"})%>

This sends the form back to the correct action. However, all the values being sent back are null.

FormCollection worked briefly with Html.BeginForm and by parsing the FormCollection dictionary but that setup stopped working for a reason I don't fully understand.

What is the issue here? I feel like I'm in webform land all over again...


回答1:


I think the problem here is that you are trying to submit two different forms to the same action. Normally when you have two forms you have two different associated view models containing the fields. So I would recommend you having two different controller actions.



来源:https://stackoverflow.com/questions/4622034/when-a-submit-button-triggers-a-second-form-the-second-forms-submit-button-sen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!