如何让我的 MVC 页面 post 在 IE 和 Chrome 中执行相同的操作?
How do I get my MVC page to post to the same action in IE and Chrome?
我有一个 MVC/Razor 站点设置,其中根据用户使用的是 Chrome 42 还是 IE 11 调用不同的操作。按钮在局部视图中。在 Chrome 中,单击按钮会将您带到分部视图 BeginForm 标记中定义的操作。在 IE 中,单击按钮会将您带到父级的 BeginForm 标记中定义的操作。为什么会发生这种情况,我如何控制在 IE 中调用什么操作?
简而言之,我有
- 一个视图,MerchantApplicationSummary,带有部分视图,PrintApplication
- PrintApplication 有一个在 Chrome 中调用 PrintApplication() 的按钮,但在 IE
中调用 MerchantApplicationSummary()
以下是一些代码示例:
我有一个局部视图。
MerchantApplicationSummary.cshtml
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "PartialDiv" }))
{
...
<div id="PartialDiv">
@{
Html.RenderPartial("PrintApplication");
}
</div>
...
}
-------------------------------------------------
| |
| MerchantApplicationSummary.cshtml |
| |
| ----------------------------------- |
| | PrintApplication.cshtml | |
| | | |
| ----------------------------------- |
| |
-------------------------------------------------
这个部分视图有一个调用操作的按钮
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" />
}
这就是问题所在。在 Chrome 中,调用了 PrintApplications。这是正确的行为。但是,在 IE 中,会调用 MerchantApplicationSummary。我最初的想法是,这是因为两个 BeginForm 语句,但我还没有找到一种方法来更改它们以使其正常工作。
AdminController.cs
[AuthorizeAdmin]
[HttpPost]
public ActionResult MerchantApplicationSummary(string[] ints, FormCollection form, int? page, string submit)
{
...
}
[AuthorizeAdmin]
[HttpPost]
public ActionResult PrintApplications(ModelApplication currentApplication)
{
}
基本上我想单击 btnPrintApplications 并让它在 Chrome 和 IE 中调用 PrintApplications 操作。
如果我可以提供更多详细信息,请告诉我。谢谢!
表单中的表单无效 HTML。那么,浏览器最终如何处理它,完全取决于各个浏览器如何呈现页面和处理错误更正,您可以 零 控制两件事。
如果您希望您的应用程序行为一致,那么您需要使用有效 HTML。换句话说,找到另一种方法来做你想做的事情,而不是在另一种形式中使用一种形式。
我有一个 MVC/Razor 站点设置,其中根据用户使用的是 Chrome 42 还是 IE 11 调用不同的操作。按钮在局部视图中。在 Chrome 中,单击按钮会将您带到分部视图 BeginForm 标记中定义的操作。在 IE 中,单击按钮会将您带到父级的 BeginForm 标记中定义的操作。为什么会发生这种情况,我如何控制在 IE 中调用什么操作?
简而言之,我有
- 一个视图,MerchantApplicationSummary,带有部分视图,PrintApplication
- PrintApplication 有一个在 Chrome 中调用 PrintApplication() 的按钮,但在 IE 中调用 MerchantApplicationSummary()
以下是一些代码示例:
我有一个局部视图。
MerchantApplicationSummary.cshtml
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "PartialDiv" }))
{
...
<div id="PartialDiv">
@{
Html.RenderPartial("PrintApplication");
}
</div>
...
}
-------------------------------------------------
| |
| MerchantApplicationSummary.cshtml |
| |
| ----------------------------------- |
| | PrintApplication.cshtml | |
| | | |
| ----------------------------------- |
| |
-------------------------------------------------
这个部分视图有一个调用操作的按钮
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" />
}
这就是问题所在。在 Chrome 中,调用了 PrintApplications。这是正确的行为。但是,在 IE 中,会调用 MerchantApplicationSummary。我最初的想法是,这是因为两个 BeginForm 语句,但我还没有找到一种方法来更改它们以使其正常工作。
AdminController.cs
[AuthorizeAdmin]
[HttpPost]
public ActionResult MerchantApplicationSummary(string[] ints, FormCollection form, int? page, string submit)
{
...
}
[AuthorizeAdmin]
[HttpPost]
public ActionResult PrintApplications(ModelApplication currentApplication)
{
}
基本上我想单击 btnPrintApplications 并让它在 Chrome 和 IE 中调用 PrintApplications 操作。
如果我可以提供更多详细信息,请告诉我。谢谢!
表单中的表单无效 HTML。那么,浏览器最终如何处理它,完全取决于各个浏览器如何呈现页面和处理错误更正,您可以 零 控制两件事。
如果您希望您的应用程序行为一致,那么您需要使用有效 HTML。换句话说,找到另一种方法来做你想做的事情,而不是在另一种形式中使用一种形式。