Return two lists to the view c# mvc
Asked Answered
V

3

10

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!

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());

    }
Virginium answered 7/11, 2013 at 15:7 Comment(2)
Do you want them to be in the same list or two seperate lists?Centurial
Well I have two html grid's in the view, one for assets added and one for assets removed, so i guess they need to remain split?Virginium
D
13

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".

Example:

Your 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
...
Duckling answered 7/11, 2013 at 15:10 Comment(1)
And as an addendum, in order to access the list in the view, you would use syntax such as '@Model.Added' and '@Model.Removed'Siskind
W
7

Why not having a ViewModel containing these 2 lists

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

I think you want the first one.

Work answered 7/11, 2013 at 15:8 Comment(1)
I wouldn't suggest ViewData, the first way, is the best way.Device
I
1

Try using the dynamic type like ViewBag

You can access @viewbag in Razor View

Impetus answered 7/11, 2013 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.