为什么我的表单会触发错误的控制器操作?
该表单有多个提交按钮,单击时,它会调用一个简单的 JavaScript 函数来更改隐藏输入的值(函数称为“setHidden”)。这之前有效,在其他一些不相关的代码之后,它已停止工作。 本质上,它应该调用的操作永远不会被调用,相反,它似乎默认返回到以前的 URL。
表单:
<form action="/League/RemoveOwner" method="post">
<input type="hidden" value="1007" name="lid"/>
<input type="hidden" value="0" id="index" name="index"/>
<input type="image" src="../../Resources/Images/Delete.png"
height="12" alt="Remove Owner" title="Remove Owner"
onclick="setHidden('index', '1031')"/></a> coach<br />
</form>
控制器:
[HttpPost]
public ActionResult RemoveOwner(int id, string index)
{
//yada
return PartialView();
}
单击图像时,它应该调用删除所有者控制器,而不是调用“视图”控制器:
public ActionResult View(int id) {
//yada
return View();
}
This form has multiple submit buttons, when clicked, it calls a simple JavaScript function to change the value of a hidden input (function is called "setHidden". This worked before, after some other not relevant code, it has ceased working.
Essentially, the action it is supposed to call is never called, instead it seems to default back to a previous URL.
The Form:
<form action="/League/RemoveOwner" method="post">
<input type="hidden" value="1007" name="lid"/>
<input type="hidden" value="0" id="index" name="index"/>
<input type="image" src="../../Resources/Images/Delete.png"
height="12" alt="Remove Owner" title="Remove Owner"
onclick="setHidden('index', '1031')"/></a> coach<br />
</form>
The Controller:
[HttpPost]
public ActionResult RemoveOwner(int id, string index)
{
//yada
return PartialView();
}
When clicking the image, it should call the remove owner controller, instead it calls the "View" controller:
public ActionResult View(int id) {
//yada
return View();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一番查找,终于找到了问题所在。该页面上还有另一个没有结束标记的表单。该表单应该将其带回“视图”控制器。因为是局部的,所以我直到……好吧,直到我绞尽脑汁思考了两个小时才发现它……
After searching high and low, I finally found the problem. There was another form on the page that didn't have a closing tag. THAT form was supposed to take it back to the "View" controller. It was on a partial, so I didn't catch it until... well until I had racked my brain for 2 hours...
对表单操作代码的另一个建议是不要编写表单操作的绝对路径。您可以使用 Html.BeginForm() 或在 web.config 中写入您的站点 url,并在应用程序从 global.ascx 启动时将其获取到 Application["URL"]。那么您的表单应该如下所示:
这种方法可以避免发布到 IIS 或其他 Web 服务器时出现一些错误。
Another suggestion for you form action code is don't write the absolute path for your form action. You could use the Html.BeginForm() or write your site url in the web.config and fetch it to the Application["URL"] while the application start at global.ascx. Then your form should look like:
This approach can avoid some errors while publishing to the IIS or another web server.