asp.net MVC ViewData详解
2014-07-30 09:42
597 查看
转自:/article/5541806.html
控制器向视图中传值ViewData详解
1.将一个字符串传值到视图中
在action中我们将字符串保存在ViewData(或ViewBag [asp.net 3或以上才可用])中代码如下:
public ActionResult Index()
{
ViewData["str1"]= "这是一个字符串";
//也可以使用ViewBag来传递值
ViewBag.str2="这是另外一个字符串";
return View();
}
在视图中我们可以用下面代码将字符串显示出来
<h1>@ViewData["str1"]</h1>
<h1>@ViewBag.str2</h1>
2.将一个字符串集合传递到视图中
public ActionResult Index()
{
List<string> str1= new List<string>();
str1.Add("1111");
str1.Add("2222");
str1.Add("3333");
ViewData["str"] = str1;
return View();
}
在视图中我们通过下面语句将str1的值显示出来
@foreach (var a in ViewData["str"] as List<string>)
{
@a
}
3.将一个datatable的值传递到视图中
public ActionResult Index()
{
DataTable newtable = new DataTable("d");
newtable.Columns.Add("商品编号", typeof(string));
newtable.Columns.Add("客户编号", typeof(string));
DataRow NewRow = newtable.NewRow();
NewRow["商品编号"] = "132323213434";
NewRow["客户编号"] = "344223443244";
newtable.Rows.Add(NewRow);
DataRow SNewRow = newtable.NewRow();
SNewRow["商品编号"] = "343432445456";
SNewRow["客户编号"] = "454523432453";
newtable.Rows.Add(SNewRow);
ViewData["dt"]= newtable;
return View();
}
在视图中我们通过下面语句将dt的值显示出来
注意:在顶部要先加上:@using System.Data;
<ul>
@foreach(DataRow dr in (ViewData["dt"] as DataTable).Rows)
{
<li>
@dr["商品编号"],@dr["客户编号"],
</li>
}
</ul>
控制器向视图中传值ViewData详解
1.将一个字符串传值到视图中
在action中我们将字符串保存在ViewData(或ViewBag [asp.net 3或以上才可用])中代码如下:
public ActionResult Index()
{
ViewData["str1"]= "这是一个字符串";
//也可以使用ViewBag来传递值
ViewBag.str2="这是另外一个字符串";
return View();
}
在视图中我们可以用下面代码将字符串显示出来
<h1>@ViewData["str1"]</h1>
<h1>@ViewBag.str2</h1>
2.将一个字符串集合传递到视图中
public ActionResult Index()
{
List<string> str1= new List<string>();
str1.Add("1111");
str1.Add("2222");
str1.Add("3333");
ViewData["str"] = str1;
return View();
}
在视图中我们通过下面语句将str1的值显示出来
@foreach (var a in ViewData["str"] as List<string>)
{
@a
}
3.将一个datatable的值传递到视图中
public ActionResult Index()
{
DataTable newtable = new DataTable("d");
newtable.Columns.Add("商品编号", typeof(string));
newtable.Columns.Add("客户编号", typeof(string));
DataRow NewRow = newtable.NewRow();
NewRow["商品编号"] = "132323213434";
NewRow["客户编号"] = "344223443244";
newtable.Rows.Add(NewRow);
DataRow SNewRow = newtable.NewRow();
SNewRow["商品编号"] = "343432445456";
SNewRow["客户编号"] = "454523432453";
newtable.Rows.Add(SNewRow);
ViewData["dt"]= newtable;
return View();
}
在视图中我们通过下面语句将dt的值显示出来
注意:在顶部要先加上:@using System.Data;
<ul>
@foreach(DataRow dr in (ViewData["dt"] as DataTable).Rows)
{
<li>
@dr["商品编号"],@dr["客户编号"],
</li>
}
</ul>
相关文章推荐
- asp.net MVC ViewData详解
- asp.net mvc中ViewData、ViewBag和TempData的详解
- asp.net MVC ViewData详解
- ASP.NET MVC 2.0 in Vs2010 :使用C# 4.0中使用动态类型来传递ViewData
- 【转】Scott_ASP.NET MVC框架(第三部分) 把ViewData从控制器传到视图
- Asp.net MVC中的ViewData与ViewBag
- [转自Scott]ASP.NET MVC框架(第三部分): 把ViewData从控制器传到视图
- ASP.NET MVC的ViewData的小知识
- ASP.NET MVC 入门5、View与ViewData
- 【转】Scott_ASP.NET MVC框架(第三部分) 把ViewData从控制器传到视图
- ASP.NET MVC框架(第三部分): 把ViewData从控制器传到视图
- ASP.NET MVC ViewData.Eval() 方法
- asp.net mvc两种传值(数据)方式(viewdata/mode)
- asp.net MVC 如何在aspx页面的head标签里输出Key为常量的ViewData呢?
- Asp.net MVC中的ViewData与ViewBag
- ASP.NET MVC 2.0 in Vs2010 :使用C# 4.0中使用动态类型来传递ViewData
- ASP.NET MVC Framework: Passing ViewData from Controllers to Views
- ASP.NET MVC框架(第三部分): 把ViewData从控制器传到视图 【转】
- Asp.net MVC中的ViewData与ViewBag(转)
- ASP.NET MVC 入门5、View与ViewData