我有两个要返回到 View 的列表,“添加”和“已删除”。但是目前我只能返回“已添加”或“已删除”。如何在一个对象中同时返回两个对象?我对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());
}
最佳答案
强烈键入您的 View ,以便将其作为关联模型,该模型是包含两个列表的自定义对象。请注意,此类对象通常称为“viewModel”。
例子:
您的viewModel:
public class MyViewModel
{
public List<X> Added { get; set; }
public List<X> Removed { get; set; }
}
您的 Controller :
public ActionResult Index()
{
var viewModel = new MyViewModel
{
Added = ...,
Removed = ...
};
return View(viewModel);
}
您的看法:
@model XXXXX.MyViewModel
...