Html.Action - 获取与发布
我经常这样做:
<div id='Product'>
@Html.Action("Create", "Product")
</div>
这很方便,因为我可以将产品创建表单的绘制委托给另一个控制器操作以嵌入到某个位置。但是,我遇到的问题是该方法有时(我还没有弄清楚在什么条件下)会调用我的控制器操作的 [HttpPost],这当然会失败。
有没有办法强制 @Html.Action() 调用 GET 版本?
I do this very often:
<div id='Product'>
@Html.Action("Create", "Product")
</div>
it's convenient because I can delegate the painting of a product creation form to another controller action for embedding in places. However, I'm having issues in that the method will sometimes (I haven't figured out under what conditions) call the [HttpPost] of my controller action, which of course fails.
Is there a way to force @Html.Action() to call the GET version?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚遇到这个问题,很难识别。我最终使用 Html.RenderPartial 代替,如下所示:
I just encountered this issue, which was hard to identify. I ended up using Html.RenderPartial instead, like this:
Html.Action
的工作方式是,如果页面的当前请求是 post 方法,那么它将搜索名为HttpPost
的方法。因此,发生的情况是,您正在 POST 当前页面,并且该操作同样假定所有应该执行的操作也必须是 POST。
据我所知,没有办法强制它切换到这样的不同方法。
The way
Html.Action
works is that if the current request for the page is a post method then it will search for the method with the nameHttpPost
.So what's happening is that you're POSTing the current page and the action likewise assumes all actions that should execute must be a POST too.
There's no way I know of to force it to switch to a different method like that.
尝试将
AcceptVerbs
属性添加到您的操作中:这适用于 GET 和 POST 请求。
Try adding the
AcceptVerbs
attribute to your action:This will work for both GET and POST requests.
我也遇到了类似的问题,确实有解决方案。
只需使用 IsPost 属性检查视图中的请求是 get 还是 POST 即可,VOILA...
向遇到类似问题的人致敬...
I also got into a similar problem and there indeed is a solution.
Just Check whether Request is get or POST in View using IsPost Property and VOILA....
Regards to whoever got in similar problem...