Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderAction calls wrong action method

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

like image 282
Adrian Grigore Avatar asked Nov 19 '10 17:11

Adrian Grigore


People also ask

Which of the following differentiates action from RenderAction?

The difference between the two is that Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.

What is the use of RenderAction in MVC?

RenderAction(HtmlHelper, String, String, RouteValueDictionary) Invokes the specified child action method using the specified parameters and controller name and renders the result inline in the parent view.

How do you change the name of an action method?

To rename the Action method of the controller, we use ActionName attribute to the action method of the controller. Here, despite that our Action method name is Index, it will be accessible from the browser as PersonList.

Can Action method static?

- Action method cannot be a static method. ActionResult is a base class of all the result type which returns from Action method.


1 Answers

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.

like image 84
Robert Koritnik Avatar answered Oct 24 '22 17:10

Robert Koritnik