Valentin V
Valentin V

Reputation: 25739

ASP.NET MVC multiple controller actions in one form

I might have wandered into wrong direction so please help.

I have a page in my ASP.NET MVC application which displays comments. These comments come from 'Index' action of 'Comments' controller. At the bottom of the page I have a form for adding new comments. This form should call

[AcceptVerbs(HttpVerbs.Get)] 
public ActionResult AddComment();

action when rendering the form to user and

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult AddComment(Comment c); 

action when posting new comment to server.

I presume that 'Comments' page should call three action methods (Index, AddComment[Get], AddComment[Post]). However AddComment[Get] is never called.

Please point me to right direction.

Thank you.

Upvotes: 1

Views: 1494

Answers (2)

Dave Van den Eynde
Dave Van den Eynde

Reputation: 17405

Add a reference to the MVC Futures assembly, and use the Html.RenderAction helper method to include the result of another action in your page view.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062780

If the form submit does a POST (submit) to the comments page (i.e. has the action of the comments page, and the method of POST), then it is indeed the POST version that chouls be used. The GET version would be used if you (for example) use a standard anchor/link to show the standalone comments page (or when issuing an AJAX "get" to that address).

How are you currently hitting the pages? At the moment it sounds like your index page is adding the form manually - so there is no place for the comments GET to be used, unless you are adding the form via AJAX.

Upvotes: 1

Related Questions