ASP.NET MVC 5通过viewdata 向视图传递多个对象
2016-03-25 11:20
633 查看
在controller查询到的数据是:
public ActionResult Index()
{
var products = GetTopSellingProducts(1);
var genres = storeDB.Genres.ToList();
ViewData["IndexProducts"] = products;
ViewData["IndexGenres"] = genres;
return View();
}
在一般的viewtag中,只能传递一个对象,此时可以使用viewdata
cshtml迭代代码如下:
@foreach (var product in ViewData["IndexProducts"] as List<Product>)
{
<li class="col-lg-2 col-md-2 col-sm-2 col-xs-4 container">
<a href="@Url.Action("Details", "Store", new { id = product.ProductId })">
<img alt="@product.ProductName" src="@Url.Content(@product.ProductImgUrl)" />
<h4>@product.ProductName</h4>
</a>
</li>
}
如果提示某个model类不在命名空间,那么就在view文件夹下的web.config中加入namespace
public ActionResult Index()
{
var products = GetTopSellingProducts(1);
var genres = storeDB.Genres.ToList();
ViewData["IndexProducts"] = products;
ViewData["IndexGenres"] = genres;
return View();
}
在一般的viewtag中,只能传递一个对象,此时可以使用viewdata
cshtml迭代代码如下:
@foreach (var product in ViewData["IndexProducts"] as List<Product>)
{
<li class="col-lg-2 col-md-2 col-sm-2 col-xs-4 container">
<a href="@Url.Action("Details", "Store", new { id = product.ProductId })">
<img alt="@product.ProductName" src="@Url.Content(@product.ProductImgUrl)" />
<h4>@product.ProductName</h4>
</a>
</li>
}
如果提示某个model类不在命名空间,那么就在view文件夹下的web.config中加入namespace
相关文章推荐
- Asp.Net Api2 过滤器的使用
- ASP.NET过滤类SqlFilter,防止SQL注入[原创]_实用技巧_脚本之家
- 中国菜刀Cknife(C刀)自定义模式秒过安全狗. jsp过狗一句话,aspx过狗一句话.
- ASP.NET Web Service如何工作(3)
- ASP.NET Web Service如何工作(2)
- ASP.NET Web Service如何工作(1)
- asp.net ashx 一般处理程序 使用async await异步直接 copy可用哦
- 请求如何进入ASP.NET MVC框架
- ASP.NET Core 1.0中的管道-中间件模式
- ASP.NET过滤类SqlFilter,防止SQL注入 原创
- 你应该知道的 asp.net webform之异步页面
- 学习asp.net
- ASP.NET中的多线程整理
- 2016-AspNet-MVC教学-6-简单类Student的初次完整创建和使用
- Windows Iot:让Raspberry Pi跑起来(1)
- Asp.Net多线程用法1
- asp.net webform 中将前台显示出来的html标签解释为排版格式
- ASP.NET Core 1.0中的管道-中间件模式
- ASP.NET在底层类库中获取Session C#类中获取Session[原创]_实用技巧_脚本之家
- Raspberry 2 B 使用记录