[英]Return two lists to the view c# mvc
I have two lists that i want to return to the view, 'added' and 'removed'. 我有两个列表,我想返回视图,'添加'和'删除'。 However currently i can only return either the 'added' or 'removed'. 但是目前我只能返回'已添加'或'已删除'。 How can i return both in one object? 我怎样才能在一个对象中返回? I am very new to MVC so apologies if this code is messy! 我对MVC很新,所以如果这段代码很乱,请道歉!
public ActionResult Index()
{
// Excel upload history
var fptexcel = db.FPTStaticDataRatedFinancialAssetBase
.OrderBy(e => e.FORATExcelId)
.Select(e => e.FORATExcelId).Max();
var fptexcelprevious = fptexcel - 1;
var newassets = db.FPTStaticDataRatedFinancialAssetBase.OfType<FPTStaticDataRatedFinancialAssetBase>()
.Where(c => c.FORATExcelId == fptexcel || c.FORATExcelId == fptexcelprevious)
.GroupBy(x => x.Name)
.Where(c => c.Count() == 1)
.Select(y => y.FirstOrDefault()).ToList();
var added = db.FPTStaticDataRatedFinancialAssetBase.OfType<FPTStaticDataRatedFinancialAssetBase>()
.Where(x => x.FORATExcelId == fptexcelprevious)
.Select(x => x).ToList();
var removed = db.FPTStaticDataRatedFinancialAssetBase.OfType<FPTStaticDataRatedFinancialAssetBase>()
.Where(x => x.FORATExcelId == fptexcel)
.Select(x => x).ToList();
return View(newassets.Except(added).ToList());
}
Strongly type your view so it as an associated model that is a custom object that contains both lists. 强烈键入您的视图,使其成为关联模型,该模型是包含两个列表的自定义对象。 Note such an object is often referred as a "viewModel". 注意,这样的对象通常被称为“viewModel”。
Example: 例:
Your viewModel: 你的viewModel:
public class MyViewModel
{
public List<X> Added { get; set; }
public List<X> Removed { get; set; }
}
Your controller: 你的控制器:
public ActionResult Index()
{
var viewModel = new MyViewModel
{
Added = ...,
Removed = ...
};
return View(viewModel);
}
Your view: 你的观点:
@model XXXXX.MyViewModel
...
Why not having a ViewModel containing these 2 lists 为什么不让ViewModel包含这两个列表
return View(new MyModel{List1 = list1,List2 = list2});
or 要么
ViewData["List1"] = list1;
ViewData["List2"] = list2;
return View();
have a look here: http://www.howmvcworks.net/OnViews/BuildingAStronglyTypedView 看看这里: http : //www.howmvcworks.net/OnViews/BuildingAStronglyTypedView
I think you want the first one. 我想你想要第一个。
Try using the dynamic type like ViewBag 尝试使用像ViewBag这样的动态类型
You can access @viewbag in Razor View 您可以在Razor View中访问@viewbag
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.