【问题标题】:How could I pass multiple collections to View as parameter from Controller in ASP.NET MVC?如何在 ASP.NET MVC 中将多个集合作为参数从 Controller 传递给 View?
【发布时间】:2019-11-28 09:15:27
【问题描述】:

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

我尝试了什么:

控制器

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

我有什么(已经工作):

控制器

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

查看

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

我应该如何更改控制器和视图以传递这些字典?

【问题讨论】:

    标签: c# asp.net asp.net-mvc razor model-view-controller


    【解决方案1】:

    您可以将多个字典包装在 DataClass 包装器中。然后在您的视图中您可以将类指定为

    View
    @model Namespace.ClassName
    
    Model
        Class YourDataClass
        {
           Dictionary 1;
           Dictionary 2;
        }
    
    Controller 
    Return View(object);
    

    【讨论】:

    • 我如何定义在视图中循环遍历的字典呢?通过Namespace.ClassName.DictionaryName 调用它?
    • @Barrosy 请看一下我的回答,看看是否有帮助。
    • @barrosy 您的模型将是完整的对象。Model.DictionaryObject 名称将是字典。要遍历它,您必须为每个循环使用。
    【解决方案2】:
    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);
    }
    

    查看:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2014-11-22
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      相关资源
      最近更新 更多