Robert Prine Robert Prine - 1 year ago 71
HTML Question

How can I return a view from another model in a controller?

I stumbled across a better way to display my comment form within the post window with

<p id="createPostButton">@Html.Action("Create", "Comment", new { id = Model.PostId })</p>
(I was originally just trying to create a button to take you to the comment page sending the post id). However, I now get an error when I try to pass back the post/details/{id} view in the CommentController. It keeps trying to look in comment or shared folders passing either post OR details/{id} instead of post/details/{id}.

Post/Details Razor HTML file:

@model FantaC.Models.Post

ViewBag.Title = @Html.DisplayFor(model => model.PostName);

<h2>@Html.DisplayFor(model => model.PostName)</h2>

<div class="row">
<div class="col-md-8 whiteBorder scroll">
<div class="postName">
<h4>Written by: @Html.DisplayFor(model => model.UserName)</h4>
<img src="@Html.DisplayFor(model => model.PostImage)" />
<div class="postContent">
<p>@Html.DisplayFor(model => model.PostContent)</p>
<div class="col-md-4 whiteBorder scroll">

@foreach (var comment in Model.PostComments)
<h5>@Html.DisplayFor(modelItem => comment.UserName)</h5>
<h5>@Html.DisplayFor(modelItem => comment.CommentSubject)</h5>
<p>@Html.DisplayFor(modelItem => comment.CommentContent)</p>
@Html.ActionLink("Edit", "../Comment/Edit", new { id = comment.CommentId }) |
@Html.ActionLink("Details", "../Comment/Details", new { id = comment.CommentId }) |
@Html.ActionLink("Delete", "../Comment/Delete", new { id = comment.CommentId })

<p id="createPostButton">@Html.Action("Create", "Comment", new { id = Model.PostId })</p> <!--**********This is the line that is important-->

@*@=Html.RenderAction("Create", "Comments", new { postId = Model.PostId });*@
@*@Html.ActionLink("Add a Comment", "Create", "Comment")*@
@Html.ActionLink("Comment", "Create", "Comment", new { id = Model.PostId }, null) |
@Html.ActionLink("Back to List", "Index")

The Comment/Create Razor HTML file that is getting pulled in by the Html.Action:

@model FantaC.Models.Comment

Layout = null;

ViewBag.Title = "Create";


@using (Html.BeginForm())

<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="form-group">
@Html.LabelFor(model => model.CommentSubject, htmlAttributes: new { @class = "control-label col-md-10 displayBlock" })
<div class="col-md-12">
@Html.EditorFor(model => model.CommentSubject, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CommentSubject, "", new { @class = "text-danger" })

<div class="form-group">
@Html.LabelFor(model => model.CommentContent, htmlAttributes: new { @class = "control-label col-md-10 displayBlock" })
<div class="col-md-12">
@Html.EditorFor(model => model.CommentContent, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CommentContent, "", new { @class = "text-danger" })

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />

@Html.ActionLink("Back to List", "Index")

@section Scripts {

The important section from the CommentController:

public ActionResult Create(string id, [Bind(Include = "CommentSubject,CommentContent")] Comment model)
if (ModelState.IsValid)
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

var commentId = (23817 + db.Comment.Count()).ToString().PadLeft(10, '0');

var comment = new Comment
CommentId = commentId,
PostId = id,
UserName = user.UserName,
PostTime = DateTime.Now,
CommentSubject = model.CommentSubject,
CommentContent = model.CommentContent


return View("Details/" + id, "Post");

return View(model);

I also tried
return View("../post/details/" + id);
to no avail. How can I get back up to the post view url (post/details/{id} from the CommentController?

As a side note, I had it almost working by taking out the returns and making the method
, but after clicking the submit comment button the whole comment/create form would disappear. I would be fine going back to that way of doing things if anyone knows a way to make the form stay after clicking the create comment button.

Thanks for any help! :)


I forgot to mention that I tried this too. It returns an error that looks like this:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Child actions are not allowed to perform redirect actions.

Source Error:

Line 32: }
Line 33:
Line 34: @Html.Action("Create", "Comment", new { id = Model.PostId })
Line 35:

Answer Source

You should follow the PRG (POST-REDIRECT-GET) pattern. After saving the comment, you should redirect to the post details page.

You may use the RedirectToAction method to return a RedirectResponse back to the browser which will make a new GET request to the post details action method.

So replace

return View("Details/" + id, "Post");


return RedirectToAction("Details" , "Post", new {id=id});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download