问题描述:

We're trying to send a list and an object to a view but we don't know

how to send multiple items to the view.

This is our view class

@model LolNexusProject.Models.TestObject

@{

ViewBag.Title = "Match History";

Layout = "~/Views/Shared/_Layout.cshtml";

}

<h2>Match History</h2>

@for (int i = 0; i < Model.matches.Count; i++)

{

<div style="background-color: #8BC34A;width: 600px; margin-bottom: 10px;">

<div style="height: 25px;background-color: #689F38; padding:5px">

<div style="float: left;">

@Model.matches[i].queueType - @(Model.matches[i].matchDuration / 60) Minutes

</div>

<div style="float: right;">

Creation date

</div>

</div>

//we want to acces the list here (we already tried the viewbag but it didn't work

</div>

}

And here is our controller class

public ActionResult Search(string name)

{

//get the data from the server

TestObject test = TestObject();"

List<long> list = getIds();

return View(test);

}

网友答案:

A common solution is to define a ViewModel class:

namespace LolNexusProject.Models
{
    public class TestObjectListViewModel
    {
        public List<long> Ids { get; set; }

        public int TestObj { get; set; }
    }
}

Make it your View Model:

@model LolNexusProject.Models.TestObjectListViewModel 

Pass an instance of it to your View from the Controller:

public ActionResult Search(string name)
{
     var viewModel = new TestObjectListViewModel {
         Ids = getIds(),
         TestObj = TestObject()
    };
    return (viewModel);
}
相关阅读:
Top