2013-02-25 79 views
0

我是MVC的新手,正在嘗試低於場景,但遇到了如何繼續。MVC控制器視圖綁定

在我的網頁中,我有幾個部分,每個部分下面有評論。和獲取的意見,我已經寫控制器內的函數如下下面這樣我就可以在視圖列表

public ActionResult ShowPreviousComments() 
     { 
      Comments com = new Comments(); 

      LstComments savedComments = new LstComments(); 

      ///Entity code here 


      foreach (var item in comments) 
      { 
       com.comments = item.Comments; 
       com.dTime = item.Time; 

       savedComments.lstCommet.Add(com); 

      } 

      return View(); 
     } 

和模型數據

public class Comments 
    { 
     public string comments { get; set; } 

     public DateTime? dTime { get; set; } 

     public int airPortId { get; set; } 

    } 

    public class LstComments 
    { 
     public List<Comments> lstCommet { get; set; } 
    } 

我的疑問是,我怎麼可以打控制器在每個部分的頁面加載過程中?

對不起,如果它聽起來愚蠢或一些錯誤。請張貼如果我能以更好的方式做到這一點

感謝

+0

你的意思是,你想在視圖中顯示評論,偏袒,對嗎? – 2013-02-25 14:29:04

+0

不是部分bcoz部分動態呈現 – user2067567 2013-02-25 14:32:41

回答

4

控制器

public ActionResult ShowPreviousComments() 
{ 
    Comments com = new Comments(); 
    LstComments savedComments = new LstComments(); 

    ///Entity code here 


    foreach (var item in comments) 
    { 
     com.comments = item.Comments; 
     com.dTime = item.Time; 

     savedComments.lstCommet.Add(com); 
    } 

    return PartialView(savedComments); 
} 

查看

@Html.Action("ShowPreviousComments", "SomeController") 

從我的角度

控制器

public ActionResult ShowPreviousComments() 
{ 
    // Entity code here 
    // var comments = entity.comments; 

    return PartialView(comments); 
} 

Index.cshtml

// some main view content 

// main view comments... 
@Html.Action("ShowPreviousComments", "SomeController") 

ShowPreviousComments.chtml(部分觀點,即保持前一評論)

@model IEnumerable<comments> 

<div class="comments_container> 
    foreach(var item in Model) 
    { 
     <div class="comment_body">@item.Comments</div> 
     <div class="comment_time">@item.Time</div> 
    } 
</div> 
+0

這是非常徹底的 – 2013-02-25 16:07:13

0

也許(包含自己的內容和評論的主要觀點) U可以嘗試一個腳本來調用控制器加載視圖的部分