2012-10-15 112 views
1

我有沒有辦法從DAL檢索數據使用LINQ澄清如下:需要在asp.net的MVC應用程序

public List<Comment> GetAllComment(int _UserID) 
    { 
     using (var context = new TourBlogEntities1()) 
     { 
      var AllComment = from s in context.Comments 
          where s.UserID == _UserID 
          select s; 

      return AllComment.ToList<Comment>(); 
     } 
    } 

現在我想從我的控制器調用此方法follwos:

[HttpPost] 
    public ActionResult NewComment(PostComment model) 
    { 
     var business = new Business(); 

     var entity = new Comment(); 
     entity.Comments = model.Comments.Comments; 
     //entity.PostID = model.Posts.PostID; HOW TO PASS POSTID??? 
     entity.UserID = business.GetUserID(User.Identity.Name); 

     business.PostComment(entity); 

     var CommentsListFromEntity = business.GetAllComment(business.GetUserID(User.Identity.Name)); 

     var viewmodel = new PostComment(); 
     viewmodel.CommentsListFromModel = CommentsListFromEntity.ToList<CommentInfo>(); // Error is showing here 

     return View("ViewPost", viewmodel); 
    } 

我將視圖與模型綁定,但無法使用列表,即使我嘗試傳遞列表本身來查看,仍然無法在視圖中使用該列表!

我的模型類如下:

public class PostComment 
{ 
    public PostComment() 
    { 
     Posts = new NewPost(); 
     Comments = new CommentInfo(); 
     User = new UserInfoModel(); 
    } 

    public NewPost Posts { get; set; } 
    public CommentInfo Comments { get; set; } 
    public UserInfoModel User { get; set; } 
    public List<CommentInfo> CommentsListFromModel { get; set; } 

} 

那麼如何使用視圖列表?我非常需要幫助!

回答

1

你需要從控制器動作返回一個ActionResult:

public ActionResult GetAllComment(int _UserID) 
{ 
    using (var context = new TourBlogEntities1()) 
    { 
     var AllComment = from s in context.Comments 
         where s.UserID == _UserID 
         select s; 

     return View(AllComment.ToList()); 
    } 
} 

現在相應的視圖可以是強類型到List<Comment>

@model List<Comment 

,然後你可以遍歷模型並顯示它:

@foreach (var comment in Model) 
{ 
    <div><@comment.Text</div> 
} 

UPDATE:

如果此方法是在你的BLL,你可以在你的控制器動作調用它,並將結果傳遞給視圖:

public ActionResult SomeAction(int userID) 
{ 
    List<Comment> comments = bll.GetAllComment(userID); 
    return View(comments); 
} 

更新2:

如果您除了此列表之外,還需要將其他屬性傳遞給視圖,然後定義包含所有必要信息的視圖模型:

public class MyViewModel 
{ 
    public List<Comment> Comments { get; set; } 
    public string Foo { get; set; } 
    public string Bar { get; set; } 
    ... 
} 

,然後讓你的控制器動作通過此視圖模型到視圖:

public ActionResult SomeAction(int userID) 
{ 
    var model = new MyViewModel(); 
    model.Comments = bll.GetAllComment(userID); 
    model.Foo = "that's the foo"; 
    model.Bar = "that's the bar"; 
    return View(model); 
} 

,最後你的看法是強類型的新的視圖模型:

@model MyViewModel 

<h2>@Html.DisplayFor(x => x.Foo)</h2> 

@foreach (var comment in Model.Comments) 
{ 
    <div>@comment.Text</div> 
} 

<span>@Html.DisplayFor(x => x.Bar)</span> 
+0

但是我不會寫方法在控制器中,這是在BLL中,現在我需要從Action方法調用方法,然後傳遞列表來查看。在這種情況下,BLL mehtod中的返回類型應該是什麼? –

+0

在這種情況下,您應該保留方法簽名,因爲您現在擁有它,然後在您的控制器操作中調用它。我已經更新了我的答案以示例。 –

+0

Darin,我錯誤地將我的帖子標記爲mvc 2,但我使用的是mvc 3,那麼請你告訴我一種綁定列表以查看使用剃刀語法的方法嗎?另一件事,我知道它不可能將多個模型綁定到同一視圖,是否可以將模型和列表綁定在一起? Thnaks。 –