问题
I have a MVC/Razor site setup where a different action is called depending on whether the user is using Chrome 42 or IE 11. The button is in a partial view. In Chrome, clicking the button takes you to the action defined in the partial views BeginForm tag. In IE, clicking the button takes you to the action defined in the parent's BeginForm tag. Why would this happen and how can I control what action is called in IE?
In brief, I have
- A View, MerchantApplicationSummary, with a partial view, PrintApplication
- PrintApplication has a button which calls PrintApplication() in Chrome, but MerchantApplicationSummary() in IE
Here are some code examples:
I have a view with a partial view.
MerchantApplicationSummary.cshtml
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "PartialDiv" }))
{
...
<div id="PartialDiv">
@{
Html.RenderPartial("PrintApplication");
}
</div>
...
}
-------------------------------------------------
| |
| MerchantApplicationSummary.cshtml |
| |
| ----------------------------------- |
| | PrintApplication.cshtml | |
| | | |
| ----------------------------------- |
| |
-------------------------------------------------
This partial view has a button which calls an action
PrintApplication.cshtml
@using (Html.BeginForm("PrintApplications", "Admin", FormMethod.Post, new { @id = "PrintApplications", @class = "form-horizontal ", @commandname = "ModelContract", @target = "_blank" }))
{
<input id="btnPrintApplications" type="submit" class="btn btn-primary" value="PrintApplications" name="PrintApplications" />
}
This is where the problem occurs. In Chrome, PrintApplications is called. This is the correct behavior. However, in IE, MerchantApplicationSummary is called. My initial thought was that it was because of the two BeginForm statements, but I haven't found a way to change them to get it to work.
AdminController.cs
[AuthorizeAdmin]
[HttpPost]
public ActionResult MerchantApplicationSummary(string[] ints, FormCollection form, int? page, string submit)
{
...
}
[AuthorizeAdmin]
[HttpPost]
public ActionResult PrintApplications(ModelApplication currentApplication)
{
}
Basically I want to click on btnPrintApplications and have it call the PrintApplications action in both Chrome and IE.
Please let me know if I can give any further details. Thank you!
回答1:
A form within a form is invalid HTML. How the browser ends up handling it, then, is entirely based on how the individual browser renders the page and handles error correction, two things you have zero control over.
If you want your application to behave consistently, then you need to use valid HTML. In other words, find another way to do what you're trying to do other than having a one form within another.
来源:https://stackoverflow.com/questions/30263628/how-do-i-get-my-mvc-page-to-post-to-the-same-action-in-ie-and-chrome