ASP.NET MVC3 C# - 在不同控制器的详细视图中编辑功能

时间:2011-08-11 08:53:57

标签: asp.net-mvc-3

我正在尝试创建一个论坛。我想在'主题细节'

中使用'post edit'的功能

我有标准的OTB线程索引视图,当你点击'详细信息'它显示OTB线程详细信息时,我添加了一个foreach来显示与该线程相关的帖子。

我现在正努力添加/允许编辑下面显示的帖子。具体显示/隐藏。

在上下文中,所有帖子都是“隐藏的”,直到管理员点击按钮“显示”帖子,反之亦然

线程控制器:

public ViewResult Details(int id)
    {

        tb_SH_Forum_Threads tb_sh_forum_threads = db.tb_SH_Forum_Threads.Single(t =>   t.Thread_ID == id);
        ViewBag.Private_ID = new SelectList(db.tb_SH_Forum_PrivateDesc, "Private_ID", "Private_Desc");
        return View(tb_sh_forum_threads);
    }

查看:

@model Shareholder_Forum.Models.tb_SH_Forum_Threads

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>tb_SH_Forum_Threads</legend>

<div class="display-label">Thread_Title</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Thread_Title)
</div>

<div class="display-label">Thread_Details</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Thread_Details)
</div>

<div class="display-label">tb_SH_Forum_Categories</div>
<div class="display-field">
    @Html.DisplayFor(model => model.tb_SH_Forum_Categories.Category_Description)
</div>

<div class="display-label">Thread_Date</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Thread_Date)
</div>

<div class="display-label">Replies</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Replies)
</div>
</fieldset>

@foreach
(var post in Model.tb_SH_Forum_Posts.Where(w => w.Private_ID == 1).OrderBy(o =>     o.Post_Date))
{

        <div class ="post">
<fieldset>
        <p class="post_details">At @post.Post_Date By @(post.Anon == true ? "Anonymous"    : post.Username)          
        </p>
        @post.Post_Desc

</fieldset>
        </div>}

<p>
   @Html.ActionLink("Back to List", "Index")|

</p>

我想我需要使用RenderAction和/或Partial视图,但我不明白。任何建议,或指出我正确的方向,我可以了解这一点。

一如既往,非常感谢。

1 个答案:

答案 0 :(得分:1)

我不确定我理解你想要什么,但是这就是你如何做我认为你在问的问题。

@foreach (var post in Model.tb_SH_Forum_Posts.Where(w => w.Private_ID == 1).OrderBy(o =>     o.Post_Date))
{
    if(post.IsEditable) //however you're determining if they can edit the post. Alternatively display both this and the else and use javascript to toggle which one you show
    {
         ///...Your old view post code
    }
    else
    {
       @Html.RenderPartial("EditPost", new {postdata = post})
    }
 }

制作模特

public class PostDataViewModel
{
     public Post PostData
     {
        get;
        set;
     }
}

EditPost.cshtml

 @model PostDataViewModel

 // The editable form and button to submit to SaveForumPost action

保存
public virtual ActionResult SaveForumPost(PostaDavaViewModel model)
{
   //... save edits
   // either return a redirect to Detail, or if you don't want to refresh the page call this with ajax
}