Take the 2-minute tour ×

I need to have two arguments in action method: one for id of article and second with model of comment.

I wrote form in article which have action Articles/{id}/AddComment.

            "Article_action", // Route name
            "Articles/{id}/{action}", // URL with parameters
            new { controller = "Articles" } // Parameter defaults

My form:

<form action="@Url.RouteUrl( new { controller = "Articles", id = article.article_id, action = "AddComment" })" method="post">
<input type="hidden" name="article_id" value="@article.article_id" />
<textarea name="comment" rows="6" cols="30"></textarea>
<input type="submit" />

Here is my ArticleViewModelResponse:

public class ArticleViewModelResponse {
    public int article_id{set;get;}
    public string comment{set;get;}

My action method:

public ActionResult DodajKomentarz( int id, ArticleViewModelResponse comment) {

Here is a problem... comment argument always has null value but id is correct. And if I change type ArticleViewModelResponse to FormCollection then comment has every variables.

Where is the problem? Why FormCollection has evethings and ArticleViewModelResponse doesnt?

P.S. Of course this is only example demonstrates my problem and it isnt all my code. So ignore every misspeling.

share|improve this question
Did you add a route for this? –  CD.. Aug 15 '11 at 8:53

3 Answers 3

up vote 1 down vote accepted

It looks the names of your input elements are wrong, you need to prefix them with the parameters name, try this:

<input type="hidden" name="comment.article_id" value="@article.article_id" />
<textarea name="comment.comment" rows="6" cols="30"></textarea>
share|improve this answer
Thanks. This is it! :) –  nosbor Aug 15 '11 at 9:09

Try creating a view model class like below.

public class ArticleViewModelResponse {     
       public int article_id{set;get;}     
       public string comment{set;get;} 
       public int Id {get; set;} 

  public ActionResult DodajKomentarz( ArticleViewModelResponse comment) 
  {   //...   } 

Now the Id will be part of view model class that will be automatically send to controller.

Thanks, -Naren

share|improve this answer
Still the same problem. Maybe should I do something else? –  nosbor Aug 15 '11 at 9:05

Add routing rule in global.asax.cs file.

share|improve this answer
Of course there is a rule. If I hadnt add rule then my action method would not have to launched. –  nosbor Aug 15 '11 at 9:04

Your Answer


