如何在ASP.NET MVC中从控制器将多个集合作为参数传递给View?

Bar*_*osy 0 c# asp.net model-view-controller asp.net-mvc razor

我正在尝试从控制器向视图发送数据集合。我设法使我的代码与单个集合一起工作。是否可以通过多个集合(不使用ViewBag而是将这些作为参数传递给View)来实现?

我试过的

控制者

public ActionResult Index()
{
    Dictionary<int, string> DictOne = MyObjOne.DictOne;
    Dictionary<int, string> DictTwo= MyObjTwo.DictTwo;
    return View(new { DictOne, DictTwo });
}
Run Code Online (Sandbox Code Playgroud)

我所拥有的(已经在工作):

控制者

public ActionResult Index()
{
    Dictionary<int, string> DictOne = MyObjOne.DictOne;
    return View(DictOne);
}
Run Code Online (Sandbox Code Playgroud)

视图

@model Dictionary<int, string>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list"></span>&nbsp;
                Test
            </div>
            <div class="panel-body">
                <table class="table">
                    <thead>
                        <tr>
                            <th>
                                Test
                            </th>                                
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (KeyValuePair<int, string> kvp in Model)
                        {
                            <tr>
                                <td>
                                    @kvp.Value
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

如何更改控制器和视图以传递这些词典?

Mat*_*att 5

  1. 创建新课程(例如ViewModel
  2. 将DictOne和DictTwo添加为属性
  3. 填满ViewModel你的字典
  4. 通过ViewModel查看

例:

public class ViewModel{
    public Dictionary DictOne {get;set;}
    public Dictionary DictTwo {get;set;}
}

public ActionResult Index()
{
    Dictionary<int, string> DictOne = MyObjOne.DictOne;
    Dictionary<int, string> DictTwo= MyObjTwo.DictTwo;

    ViewModel model = new ViewModel(){
    DictOne = DictOne,
    DictTwo = DictTwo
    };
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model ViewModel

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list"></span>&nbsp;
                Test
            </div>
            <div class="panel-body">
                <table class="table">
                    <thead>
                        <tr>
                            <th>
                                Test
                            </th>                                
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (KeyValuePair<int, string> kvp in Model.DictOne)
                        {
                            <tr>
                                <td>
                                    @kvp.Value
                                </td>
                            </tr>
                        }

                        @foreach (KeyValuePair<int, string> kvp in Model.DictTwo)
                        {
                            <tr>
                                <td>
                                    @kvp.Value
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)