简体   繁体   English

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

[英]How could I pass multiple collections to View as parameter from Controller in ASP.NET MVC?

I am trying to send a collection of data to a View from a Controller. 我正在尝试从控制器向视图发送数据集合。 I managed to make my code work with a single collection. 我设法使我的代码与单个集合一起工作。 Is it possible to achieve this with multiple collections (not using ViewBag but rather passing these as parameters to the View)? 是否可以通过多个集合(不使用ViewBag而是将这些作为参数传递给View)来实现?

What I tried: 我试过的

Controller 控制者

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

What I have (working already): 我所拥有的(已经在工作):

Controller 控制者

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

View 视图

@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>

How should I change the controller and view to pass these dictionaries? 如何更改控制器和视图以传递这些词典?

  1. Create new class (eg. ViewModel ) 创建新的类(例如ViewModel
  2. Add DictOne and DictTwo as properties 将DictOne和DictTwo添加为属性
  3. Fill ViewModel with your dictionaries 用字典填充ViewModel
  4. Pass ViewModel to view 传递ViewModel进行查看

Example: 例:

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

View: 视图:

@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>

You can wrap the multiple dictionaries in a DataClass wrapper.Then in you View you can specify the class as 您可以将多个字典包装在DataClass包装器中。然后在View中可以将类指定为

View
@model Namespace.ClassName

Model
    Class YourDataClass
    {
       Dictionary 1;
       Dictionary 2;
    }

Controller 
Return View(object);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC将参数从视图传递到控制器 - ASP.NET MVC pass a parameter from a view to a controller ASP.NET MVC传递多个模型从控制器查看 - ASP.NET MVC Pass Multiple Models to View From Controller 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC 我如何使用 datapost 将参数从 jqgrid 传递到 controller(使用 MVC4 和 asp.net) - how can i pass parameter from jqgrid to controller with datapost (using MVC4 and asp.net ) 从视图向控制器传递多个数据集合(ASP.NET MVC 5) - Passing multiple Collections of data from View to Controller (ASP.NET MVC 5) 如何使用POST将复杂对象从视图传递到控制器-ASP.NET MVC - How can i pass complex object from view to controller with POST - ASP.NET MVC 如何将对象从我的视图传递到 ASP.NET MVC 中的控制器? - How can I pass an object from my view to my controller in ASP.NET MVC? 如何将隐藏字段值从视图传递到控制器 ASP.NET MVC 5? - How can I pass hidden field value from view to controller ASP.NET MVC 5? 如何将DateTime参数从View传递到ASP.net MVC5中的Controller? - How do I pass DateTime parameters from View to Controller in ASP.net MVC5? 从Controller传递Html字符串以查看ASP.Net MVC - Pass Html String from Controller to View ASP.Net MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM