I am trying to post an item to a collection that belongs to another model. This is the form in the view:
@using (Html.BeginForm("Create", "Comment", FormMethod.Post, new { enctype = "multipart/form-data", encoding = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
<input type="hidden" value="@Model.DeviceID" />
<div class="form-group">
<h5>Your name</h5>
<div class="col-md-10">
<input name="Author" />
</div>
</div>
<div class="form-group">
<h5>Comment</h5>
<div class="col-md-10">
<textarea name="Comment"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Submit" /> <!--Next</input>-->
</div>
}
and the controller code is set up as follows
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(string returnUrl, int devID, string Author, string text)
{
string url = this.Request.UrlReferrer.AbsolutePath;
Comment comment = new Comment();
if (ModelState.IsValid)
{
//db.Comments.Add(comment);
db.SaveChanges();
return Redirect(url);
}
return Redirect(url);
}
However this doesnt work because I cant set the model id of the item that is getting posted.
What is the Asp.net MVC way to add to a virtual collection that a model owns?