Reputation: 113
I am building an application that contains a simple form to add Comments to Posts.
As other components in my project allow users to submit comments and use this form, I have decided to contain the form within a partial view, as so:
@model Assignment_3.ViewModels.DetailViewModel
@using (Html.BeginForm("AddComment", "Comments", new { m = Model }))
{
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(m => m.CommentSubmission.Translation, new { @class = "control-label" })
@Html.TextBoxFor(m => m.CommentSubmission.Translation, new { @class = "form-control" })
</div>
</div>
<div class="col-md-2">
<div class="form-group">
@Html.LabelFor(m => m.CommentSubmission.LanguageId, new { @class = "control-label" })
@Html.DropDownListFor(m => m.CommentSubmission.LanguageId,
Model.CommentSelectListItems, "", new { @class = "form-control" })
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
@Html.TextAreaFor(m => m.CommentSubmission.Body, 7, 15, new { @class = "form-control" })
</div>
</div>
</div>
<div class="col-md-1">
<input type="submit" class="btn btn-danger" value="AddComment" />
</div>
}
Views that link to this partial view pass their ViewModel (DetailViewModel) to it. This model contains information about the Post, such as PostId, that the comments relate to.
The main View links to the partial view as so:
@Html.Partial("_AddComment", Model);
Once the user submits, the form calls it's action method returning the view it inherited from the "Post" page:
(Controller)
[HttpPost]
public ActionResult AddComment(DetailViewModel view)
{
if (ModelState.IsValid)
{
_context.CommentSubmissions.Add(view.CommentSubmission);
_context.SaveChanges();
return RedirectToAction("Detail", "Posts", new { id = view.CommentSubmission.IdiomId });
}
view.Init(_context);
return View(view);
}
What I've noticed happening is that whilst the ViewModel does return with the information collected in the form, it does not carry over the original information from the Post's DetailViewModel. Consequently, when I call "RedirectToAction", id is null as information concerning the PostID has been lost.
I'm still new new to MVC and help persisting that / or other optimized approaches would appreciated.
ANSWER (thanks to msoliman):
Added to Form:
@Html.HiddenFor(m => m.Post.PostId)
Updated Controller to:
[HttpPost]
public ActionResult AddTranslation(DetailViewModel view)
{
if (ModelState.IsValid)
{
view.CommentSubmission.IdiomId = view.Post.PostId;
_context.TranslationSubmissions.Add(view.CommentSubmission);
_context.SaveChanges();
return RedirectToAction("Detail", "Posts", new { id = view.CommentSubmission.PostId });
}
//view.Init(_context);
return View(view);
}
Upvotes: 1
Views: 330
Reputation: 23876
I think I understood your question, however if I misunderstood please leave a comment and I would be more than happy to update me answer and help you.
You're trying to send data of post while your form only has comments fields, if you want to add field from post add a hidden field of the data you want to pass to the form. also make sure your comment view model has this field.
Add for example something like this inside your form
@Html.HiddenFor(m => m.PostID)
Also you should've defined PostID
field inside it Comment's View model (DetailViewModel
) to be passed from your view or form action to the controller.
Upvotes: 1