present多个IEnumberables和单一的WebGrid单一值的属性 [英] Present multiple IEnumberables and single value properties in single webgrid

查看:173
本文介绍了present多个IEnumberables和单一的WebGrid单一值的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含不仅自身领域,但几个参考标识的其他类的Inventory类。

I have an Inventory Class that contains not only its own fields but several reference IDs to other classes.

public class Inventory {

  public int Id { get; set; }

  public string RtNum { get; set; }

  public string AcntNum { get; set; }

  public string CardNum { get; set; }

  public string Num { get; set; }

  [Range(1,3)]
  public int Type { get; set; }

  public int CompanyId { get; set; }

  public int BranchId { get; set; }

  public int PersonId { get; set; }   }

在我的行为我产生来自其他类的相关领域的几个IEnumerable的名单。我也有一些非列表值我想传递给视图。我知道如何创建一个视图模型通过一切交给的WebGrid,但没有通过迭代列表方式。我也知道如何自动地图索引到一个列表,请参阅如何显示MVC中的WebGrid行号的。
你会如何​​将二者结合起来,让你可以使用索引,通过多个列表迭代?

In my action I generate several IEnumerable lists of the relevant fields from the other classes. I also have several non-list values I want to pass to the View. I know how to create a ViewModel to pass everything to the webgrid but have no way of iterating through the lists. I also know how to AutoMap an index to one list, see How to display row number in MVC WebGrid. How would you combine the two so that you could use the index to iterate through multiple lists?

更新#1(详细)

public class Company {
  public int Id { get; set; } 
  public string Name { get; set; }   }

public class Branch {
  public int Id { get; set; } 
  public string Name { get; set; }   }

public class Person  {
  public int Id { get; set; } 
  public string Name { get; set; }   }

public class MyViewModel  {
  public int PageNumber { get; set; }
  public int TotalRows { get; set; }
  public int PageSize { get; set; }
  public IEnumerable<Inventory> Inventories { get; set; }
  public int Index { get; set; }
  public IEnumerable<string> CmpNm { get; set; }
  public IEnumerable<string> BrnNm { get; set; }
  public IEnumerable<string> PrnNm { get; set; }        }

控制器

public class InventoryController : Controller
{  // I have a paged gird who’s code is not relevant to this discussion but a pagenumber,
   //  pagesize and totalrows will be generated
   private ProjectContext _db = new ProjectContext();

   public ActionResult Index()  {
     IEnumerable<Inventory> inventories = _db.Inventories;
     List<string> cmpNm = new List<string>; List<string> brnNm = new List<string>; List<string>     prnNm = new List<string>;
     foreach (var item in inventories) { string x1 = ""; 
     Company cmps = _db. Company.SingleOrDefault(i => i.Id == item.CompanyId); if (cmps!= null)
      { x1 = cmps.Name; } cmpNm.Add(x1); x1 = "";
     Branch brns = _db. Branch.SingleOrDefault(i => i.Id == item. Branch Id); if (brns!= null) { x1 = brns.Name; } brnNm.Add(x1); x1 = "";
     Person pers = _db.Persons.SingleOrDefault(i => i.Id == item. PersonId);
      if (pers!= null) { x1 = pers.Name; } prnNm.Add(x1); 

     // the MyViewModel now needs to populated with all its properties and generate an index
     // something along the line of 
     new MyViewModel { PageNumber= pagenumber, PageSize= pagesize,  TotalRows=Totalrows, Inventories = inventories;  CmpNm=cmpNm, BrnNm=brnNm, PrnNm=prnNm}

查看(如何创建索引的问题)

View (How to create the Index is the problem)

@model.Project.ViewModels.MyViewModel
@{ var grid = new WebGrid(Model.Inventories, Model.TotalRows, rowsPerPage: Model.PageSize); }
@grid.GetHtml( columns: grid.Columns( 
    Grid.Column("PrnNm", header: "Person", format: @Model.PrnNm.ElementAt(Index))
    Grid.Column("BrnNm", header: "Branch", format: @Model.BrnNm.ElementAt(Index))
    Grid.Column("CmpNm", header: "Company", format: @Model.CmpNm.ElementAt(Index))
    grid.Column("RtNum", header: "Route"), 
    grid.Column("AcntNum", header: "Account"), 
    grid.Column("CardNum", header: "Card")
    …      )    )

电网看起来应该像什么是不言而喻的。

What the grid should look like is self-evident.

推荐答案

这是pretty不清楚什么是你的目标。但不管它是什么,我会建议你来定义反映你的观点的要求,只包含的信息,你有兴趣在此网格看到一个真正的视图模型:

It's pretty unclear what is your goal. But no matter what it is I would recommend you to define a real view model reflecting the requirements of your view and containing only the information you are interested in seeing in this grid:

public class InventoryViewModel
{
    public int Id { get; set; }
    public string PersonName { get; set; }
    public string BranchName { get; set; }
    public string CompanyName { get; set; }
    public string RouteNumber { get; set; }
    public string AccountNumber { get; set; }
    public string CardNumber { get; set; }
}

现在,你可以拥有主视图模型:

Now you could have the main view model:

public class MyViewModel
{
    public int PageNumber { get; set; }
    public int TotalRows { get; set; }
    public IEnumerable<InventoryViewModel> Inventories { get; set; }
}

好吧,认为现在是显而易见的:

Alright, the view is now obvious:

@model MyViewModel

@{ 
    var grid = new WebGrid(
        Model.Inventories, 
        rowsPerPage: Model.PageSize
    ); 
}

@grid.GetHtml( 
    columns: grid.Columns( 
        grid.Column("Id", header: "Inventory id"),
        grid.Column("PersonName", header: "Person"),
        grid.Column("BranchName", header: "Branch"),
        grid.Column("CompanyName", header: "Company"),
        grid.Column("RouteNumber", header: "Route"), 
        grid.Column("AccountNumber", header: "Account"), 
        grid.Column("CardNumber", header: "Card")
    )    
)

现在所有剩下的就是建立在控制器此视图模型。因为我不知道你是什么想在这里实现,是否需要内连接或这些列左外连接做,我将在这里举例的左外连接:

Now all that's left is build this view model in your controller. Since I don't know what you are trying to achieve here, whether you need an inner join or a left outer join on those columns, I will take as an example here a left outer join:

public ActionResult Index()
{
    var inventories = 
        from inventory in _db.Inventories
        join person in _db.Persons on inventory.PersonId equals person.Id into outerPerson
        join company in _db.Companies on inventory.CompanyId equals company.Id into outerCompany
        join branch in _db.Branch on inventory.BranchId equals branch.Id into outerBranch
        from p in outerPerson.DefaultIfEmpty()
        from c in outerCompany.DefaultIfEmpty()
        from b in outerBranch.DefaultIfEmpty()
        select new InventoryViewModel
        { 
            PersonName = (p == null) ? string.Empty : p.Name,
            CompanyName = (c == null) ? string.Empty : c.Name,
            BranchName = (b == null) ? string.Empty : b.Name,
            Id = inventory.Id,
            AccountNumber = inventory.AcntNum,
            CardNumber = inventory.CardNum,
            RouteNumber = inventory.RtNum
        };


    var model = new MyViewModel
    {
        PageSize = 5,
        // TODO: paging
        Inventories = inventories.ToList()
    };

    return View(model);
}

这就是pretty多少呢。当然,在这个例子中我离开存货收集的分页你。它应该是pretty琐碎现在 .Skip()。取()的记录数你需要的。

正如你所看到ASP.NET MVC是非常简单的。您可以定义视图模型来反映你所需要的在视图中显示,然后填充在控制器上的这个视图模型的精确要求。大多数人避免视图模型,因为它们不填充它们,可能是由于缺乏他们所使用的基础数据访问技术的知识。正如你可以看到在这个例子难度并不在ASP.NET MVC说谎的。它位于LINQ查询。但LINQ严格无关MVC。这是应该除了MVC教训。当你正在做的MVC一直认为,鉴于模式方面,你需要present什么样的信息给用户。不要认为你有什么在你的数据库或条款无论本信息来自何方。

As you can see ASP.NET MVC is extremely simple. You define a view model to reflect the exact requirements of what you need to show in the view and then populate this view model in the controller. Most people avoid view models because they fail to populate them, probably due to lack of knowledge of the underlying data access technology they are using. As you can see in this example the difficulty doesn't lie in ASP.NET MVC at all. It lies in the LINQ query. But LINQ has strictly nothing to do with MVC. It is something that should be learned apart from MVC. When you are doing MVC always think in terms of view models and what information you need to present to the user. Don't think in terms of what you have in your database or wherever this information should come from.

这篇关于present多个IEnumberables和单一的WebGrid单一值的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆