MVC5 / C#:传递LINQ内部连接查询以查看模型

时间:2017-04-20 11:28:21

标签: c# mysql asp.net-mvc linq asp.net-mvc-4

我正在尝试将内部联接查询中的数据(.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 个答案:

答案 0 :(得分:2)

您需要创建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 :(得分:0)

我需要将不同的对象传递给视图,基本上有两个选项:

  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);
    
  2.     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