簡體   English   中英

MVC5 / C#:傳遞LINQ內部Join查詢以查看模型

[英]MVC5/C#: Pass LINQ inner Join query to view model

我試圖將內部聯接查詢中的data(.tolist)傳遞給viewmodel,以在視圖中使用這些數據,其中還包含需要viewModel數據的局部視圖。

public ActionResult IndexAdmin()
{
    int userId = (int)Session["UserID"];
    userInfo = _context.UserInfo.Find(userId);    

    var AllTours= (from p in _context.PostsInfo  //Why this doesn't return two records
                           join r in _context.Region
                           on p.RegionId equals r.Id
                           where r.CountryId == 1
                           select new
                           {
                              RegionName = r.CountryId,
                              ImageName = p.ImageName,

                           }).Distinct().ToList(); 



//Here i need to define IndexAdminViewModel to populate tours and userInfo.username 

    return View(AllTours);
}

這是IndexAdminViewModel:

 public class IndexAdminViewModel
 {
    public string UserName { get; set; }
    public string RegionName{get;set;}
    public string ImageName {get;set;}
 }

視圖(IndexAdmin.cshtml)

@Html.Partial("_BarPartialAdmin", Model)

foreach (var post in Model)
{
    <img src="~/images/@post.ImageName" alt="QfirstImage">
    <h2>post.RegionName</h2>

}

部分視圖將只需要用戶名顯示一次,因此我習慣於將模型傳遞給部分視圖以使用username屬性,RegionName和ImageName是一個集合,這樣我就可以遍歷它們並獲取一些值就像在桌子上使用它們一樣。

我的問題是如何將內部聯接查詢結果和theuserinfo.username傳遞給viewModel以在視圖中使用它們?

您需要創建2個視圖模型

public class ToursViewModel
{
    public string RegionName { get; set; }
    public string ImageName {get; set; }  
}
public class IndexAdminViewModel
{
    public string UserName { get; set; }
    public IEnumerable<ToursViewModel> Tours {get;set;}
}

然后在控制器中

public ActionResult IndexAdmin()
{
    int userId = (int)Session["UserID"];
    userInfo = _context.UserInfo.Find(userId);    

    IEnumerable<ToursViewModel> tours = (
        from p in _context.PostsInfo
        join r in _context.Region
        on p.RegionId equals r.Id
        where r.CountryId == 1
        select new ToursViewModel
        {
            RegionName = r.CountryId,
            ImageName = p.ImageName
        });
    IndexAdminViewModel model = new IndexAdminViewModel
    {
        UserName = userInfo.username,
        Tours = tours
    };
    return View(model);

並認為

@model IndexAdminViewModel
....
<h1>@Model.UserName</h1>
@foreach (var tour in Model.Tours)
{
    <img src="~/images/@tour.ImageName" alt="QfirstImage">
    <h2>@tour.RegionName</h2>
}

我需要將不同的對象傳遞給視圖,您基本上有兩個選擇:

  1. 創建一個復合類並將其用作模型

     var allTours= from p in _context.PostsInfo //Why this doesn't return two records join r in _context.Region on p.RegionId equals r.Id where r.CountryId == 1 select new PostAndRegion { Region = r.CountryId, Post= p.ImageName, }; var model = new MyCompositeModel { PostsAndRegions = allTours.ToArray(), UserInfo = null // or get from where you want to }; return View(model); 

    public class PostAndRegion
    {
        public Post Post{get;set;}
        public Region Region {get;set;}
    }

    public class MyCompositeModel
    {
        public IList<PostAndRegion> PostsAndRegions{get;set;}
        public UserInfo MyUserInfo{get;set;}
    }
  1. 將一些數據放在ViewBag中。 參見http://www.tutorialsteacher.com/mvc/viewbag-in-asp.net-mvc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM