When a submit button triggers a second form, the s

2019-09-03 21:26发布

问题:

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.