RenderAction 调用错误的操作方法

发布于 2024-10-03 14:11:42 字数 613 浏览 0 评论 0原文

我正在与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

快乐很简单 2024-10-10 14:11:42

子操作使用其父操作的 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.

娇柔作态 2024-10-10 14:11:42

这甚至无法编译:

public virtual ActionResult Edit(UserViewModel model) {}

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(UserViewModel model) {}

同一个类上不能有两个具有相同名称和相同参数的方法。另外为什么你的操作是虚拟的?


更新:

无法重现。情况似乎并非如此:

public class UserViewModel
{
    public int Id { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Edit(int id)
    {
        return View(new UserViewModel());
    }

    [HttpPost]
    public ActionResult Edit(UserViewModel model)
    {
        return View(model);
    }
}

Index.cshtml 渲染中,编辑操作调用正确的编辑操作(带有 id 参数的操作):

@{Html.RenderAction("edit", "home", new { id = "123" });}

This won't even compile:

public virtual ActionResult Edit(UserViewModel model) {}

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Edit(UserViewModel model) {}

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:

public class UserViewModel
{
    public int Id { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Edit(int id)
    {
        return View(new UserViewModel());
    }

    [HttpPost]
    public ActionResult Edit(UserViewModel model)
    {
        return View(model);
    }
}

And in Index.cshtml render the edit action calls the correct Edit action (the one with id parameter):

@{Html.RenderAction("edit", "home", new { id = "123" });}
草莓味的萝莉 2024-10-10 14:11:42

我不是 100% 确定这在 MVC3 中是否可用,但在 MVC2(使用 MvcFutures:Microsoft.Web.MVC)中我会使用:

Html.RenderAction<UsersController>(c => c.Edit(666));

I'm not 100% sure if this is available in MVC3, but in MVC2 (with MvcFutures: Microsoft.Web.MVC) I would use:

Html.RenderAction<UsersController>(c => c.Edit(666));
不奢求什么 2024-10-10 14:11:42

我知道这已经非常古老了,而且我们现在使用的是 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 my return Edit(), and if they weren't I called AntiForgery.Validate() to properly validate the token.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文