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>
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)
如何更改控制器和视图以传递这些词典?
ViewModel)ViewModel你的字典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>
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)
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |