如何从控制器中的其他模型返回视图?

时间:2016-08-11 18:36:37

标签: c# html asp.net-mvc razor

我偶然发现了一种更好的方法,可以在帖子窗口中使用<p id="createPostButton">@Html.Action("Create", "Comment", new { id = Model.PostId })</p>显示我的评论表单(我原本只是想创建一个按钮,将您带到发送帖子ID的评论页面)。但是,当我尝试传回CommentController中的post / details / {id}视图时,我现在收到错误。它不断尝试查看评论或共享文件夹,通过帖子或详细信息/ {id}而非帖子/详细信息/ {id}。

发布/详细信息Razor HTML文件:

@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>
        <div class="postContent">
            <p>@Html.DisplayFor(model => model.PostContent)</p>
        </div>
    </div>
    <div class="col-md-4 whiteBorder scroll">
        <h4>Comments</h4>

        @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>
                <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>
        }

        <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.Partial("Comments")*@
    </div>
</div>
<p>
    @*@Html.ActionLink("Add a Comment", "Create", "Comment")*@
    @Html.ActionLink("Comment", "Create", "Comment", new { id = Model.PostId }, null) | 
    @Html.ActionLink("Back to List", "Index")

由Html.Action提取的评论/创建Razor HTML文件:

@model FantaC.Models.Comment

@{
    Layout = null;
}

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Comment</h4>
        <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>
        </div>

        <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>
        </div>

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

@*<div>
    @Html.ActionLink("Back to List", "Index")
</div>*@

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

CommentController的重要部分:

    [HttpPost]
    [ValidateAntiForgeryToken]
    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
            };

            db.Comment.Add(comment);
            db.SaveChanges();

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

        return View(model);
    }

我也试过return View("../post/details/" + id);无济于事。如何从评论控件中回到帖子视图网址(post / details / {id}?

作为旁注,我通过取出返回并制作方法void几乎完成了工作,但在点击提交注释按钮后,整个注释/创建表单将消失。如果有人知道在点击创建评论按钮后让表格保持不变的话,我会回到那种做事的方式。

感谢您的帮助! :)

编辑:

我忘了提到我也试过这个。它返回一个如下所示的错误:

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidOperationException:不允许子操作执行重定向操作。

来源错误:

第32行:} 第33行: 第34行:@ Html.Action(“创建”,“评论”,新{id = Model.PostId})

第35行:

1 个答案:

答案 0 :(得分:2)

您应该遵循 PRG POST-REDIRECT-GET )模式。保存评论后,您应该重定向到帖子详细信息页面。

您可以使用RedirectToAction方法将RedirectResponse返回给浏览器,该浏览器将对帖子详细信息操作方法发出新的GET请求。

所以替换

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

return RedirectToAction("Details" , "Post", new {id=id});
相关问题