RenderAction calls wrong action method

社会主义新天地 提交于 2019-12-05 08:09:47

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.

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" });}

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));

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!