RenderAction 调用错误的操作方法
我正在与 renderaction 作斗争,问题是它在我的控制器上调用了错误的操作方法。
在我的“Users”控制器上,有两种称为 edit 的操作方法,一种用于 get,一种用于 post 请求:
public virtual ActionResult Edit(int id)
{
//return a view for editing the user
}
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(UserViewModel model)
{
//modify the user...
}
在我看来,我按如下方式调用 Renderaction:
Html.RenderAction("Edit", "Users", new { id = 666});
现在的问题是我希望呈现 GET 操作方法。但是(也许是因为模型还包含一个名为 ID 的属性?),Renderaction 改为调用我的 POST 操作方法。
执行此操作的正确方法是什么?我正在使用 ASP.NET MVC 3 RC,以防万一。
谢谢,
阿德里安
I'm struggling with renderaction, the problem is that it calls the wrong action method on my controller.
On my "Users" controller there are two action methods called edit, one for get and one for post requests:
public virtual ActionResult Edit(int id)
{
//return a view for editing the user
}
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(UserViewModel model)
{
//modify the user...
}
In my view, I'm calling Renderaction it as follows:
Html.RenderAction("Edit", "Users", new { id = 666});
Now the problem is that I want the GET action method to be rendered. However (perhaps because the model also contains a property called ID?), Renderaction calls my POST action method instead.
What's the proper way to do this? I'm using ASP.NET MVC 3 RC in case it matters.
Thanks,
Adrian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
子操作使用其父操作的 HTTP 方法
问题是您的视图是在回发操作之后呈现的。视图中的所有子操作渲染都使用相同的 HTTP 方法。所以 POST 被复制到它们身上。我不确定 MVC3,但在 MVC2 中没有内置方法来克服这个问题。
所以问题是您希望您的
Edit()
操作在 POST 视图上呈现为 GET。开箱即用。决不。您当然可以通过提供自己的功能=类来做到这一点。
Sub action uses HTTP method of its parent action
The problem is that your view is being rendered after a postback action. All sub-action renderings in the view use the same HTTP method. So POST is being replicated on them. I'm not sure about MVC3, but in MVC2 there was no built-in way to overcome this problem.
So the problem is that you want your
Edit()
action to be rendered as a GET on a POST view. Out of the box. No way.You can of course do it by providing your own functionality = classes.
这甚至无法编译:
同一个类上不能有两个具有相同名称和相同参数的方法。另外为什么你的操作是虚拟的?
更新:
无法重现。情况似乎并非如此:
在
Index.cshtml
渲染中,编辑操作调用正确的编辑操作(带有 id 参数的操作):This won't even compile:
You cannot have two methods with the same name and same arguments on the same class. Also why your actions are
virtual
?UPDATE:
Unable to repro. This doesn't seem to be the case:
And in
Index.cshtml
render the edit action calls the correct Edit action (the one with id parameter):我不是 100% 确定这在 MVC3 中是否可用,但在 MVC2(使用 MvcFutures:Microsoft.Web.MVC)中我会使用:
I'm not 100% sure if this is available in MVC3, but in MVC2 (with MvcFutures: Microsoft.Web.MVC) I would use:
我知道这已经非常古老了,而且我们现在使用的是 MVC5 - 但这仍然是运行
Html.RenderAction()
时表现出的行为。我对这种特殊情况的解决方案是在我的
[HttpPost]
操作中检查视图模型上的值,如果它们为空(或其他),我调用我的return Edit( )
,如果不是,我会调用AntiForgery.Validate()
来正确验证令牌。I know this is extremely old, and we're on MVC5 now - but this is still the behavior exhibited when running
Html.RenderAction()
.My solution to this particular case, was to make a check in my
[HttpPost]
action for values on my view model, and if they were null (or whatever) I called myreturn Edit()
, and if they weren't I calledAntiForgery.Validate()
to properly validate the token.