2011-08-11 35 views
0

我試圖創建一個論壇。我試圖在'線程詳細信息'中具有「發佈編輯」的功能ASP.NET MVC3 C# - 編輯不同控制器的詳細視圖功能

我有標準的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和/或部分視圖,但我不明白。任何建議,或指出我在正確的方向,我可以瞭解這一點。

一如既往,非常感謝。

回答

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 
} 
+0

非常感謝你的回覆。我在模型部分有點迷茫(因爲我必須首先製作應用程序數據庫,並且我沒有使用代碼模型的經驗),但其餘部分是合理的! – Amy

+0

感謝您的幫助! – Amy

+0

沒問題。模型應該是任何需要從PartialView中識別和編輯帖子的信息。我不知道你給你的類命名,所以我只是把它稱爲Post。如果任何不清楚的只是說出哪一部分。 – DMulligan

相關問題