I'm a new learner of ASP.NET MVC. This problem should be easy to answer for professions. I'm trying to submit a form (post comment), and getting error.
Here is my controller
[HttpPost]
public ActionResult Index(long id, CommentViewModel comment) {
var service = new UserService();
var articleService = new ArticleService();
comment.UserId = service.GetUserByUsername(User.Identity.Name).UserId;
comment.ArticleId = id;
comment.CommentTime = DateTime.Now;
articleService.AddArticleComment(comment);
return View();
}
here is my ViewModel
public class CommentViewModel : BaseViewModel {
public Int64 CommentId { get; set; }
public Int64 UserId { get; set; }
public Int64 ArticleId { get; set; }
public DateTime CommentTime { get; set; }
public String CommentBody { get; set; }
}
here is my View (HTML)
<form class="form-stacked" id="comment-form" action="@Url.Action("Index", "Article")" method="post" enctype="multipart/form-data">
<label class="control-label" for="commentTextArea">Leave a comment...</label>
<textarea rows="3" id="commentTextArea" name="commentBody" class="span8"></textarea>
<input type="submit" value="Post Comment" />
</form>
this is URL
http://localhost:58856/Article?Id=1
and this is the Error Message
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult Index(Int64, TechCells.Services.ViewModels.CommentViewModel)' in 'TechCells.Web.UI.Controllers.ArticleController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
I guess MVC should automatically detect the id from the URL, and automatically assign ViewModel properties based on names. Right?
What is the reason for this error? How can I fix?
Thanks.