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.
routes.MapRoute(
"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" />
</form>
Here is my ArticleViewModelResponse:
public class ArticleViewModelResponse {
public int article_id{set;get;}
public string comment{set;get;}
}
My action method:
[HttpPost]
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.
It looks the names of your input elements are wrong, you need to prefix them with the parameters name, try this: