ASP.NET MVC中的数据传递


前言

在Web开发中页面数据的传输尤为重要,而在MVC框架中的数据传输更显突出,本博客简单讲解了四种ASP.NET的MVC框架中使用的对象及案例

一、ViewData对象

  1. ViewData是一种字典集合数据,是“视图基类”和“控制器基类”的属性
  2. 常见的用法是在控制器中写入数据,在视图中读取数据
  3. ViewData的value可以存放任意数据类型的数据,因此使用时需要强制转换
    在控制器中:
		public ActionResult Index()
        {
            List<Student> stulist = server.GetStudents();
            ViewData["stulist"] = stulist;
            return View();
        }

在视图页面中:

<table border="1" cellpadding="0" cellspacing="0">
	<tr>
       <th>学号</th>
       <th>姓名</th>
       <th>性别</th>
       <th>年龄</th>
       <th>查看</th>
       <th>修改</th>
       <th>删除</th>
	</tr>
            @{ 
                List<Student> list = ViewData["stulist"] as List<Student>;
                if (list.Count>0)
                {
                    foreach (Student item in list)
                    {
                        <tr>
                            <td>@item.Id</td>
                            <td>@item.Name</td>
                            <td>@item.Sex</td>
                            <td>@item.Age</td>
                            @*<td>@Html.ActionLink("详情", "Deatil", "Home", new { Id=item.Id},new { })</td>*@
                            <td><a href="~/Home/[email protected]">修改</a></td>
                            <td><a href="~/Student/[email protected]">修改</a></td>
                            <td><a href="#">删除</a></td>
                        </tr>
                    }
                }
            }
	<form action="~/Home/Deatil" method="post">
            <input type="text" name="Id" value="" /><br />
            <input type="submit" name="name" value="查看" />
	</form>
</table>

二、ViewBag对象

  1. ViewBag是dynamic类型的对象,同样也是“视图基类”和“控制器基类”的属性
  2. 好处:使用灵活方便
  3. 特点:ViewBag其实是对ViewData数据的包装,使用ViewData保存数据可以使用ViewBag读取,反之也是如此
  4. 实际开发中最好选择其中一种使用,建议使用ViewBag
		public ActionResult Deatil(int Id)
        {
            int id = Convert.ToInt32(Request["Id"]);
            //根据Id获取对应的对象信息,因此需要从客户端的请求中获取数据
            Student student = server.GetStudentById(Id);
            if (student != null)
            {
                ViewBag.Stu = student;
                return View();
            }
            else
            {
                return View("Error");
            }
        }
<body>
    <div> 
    	<h1>学员【@ViewBag.Stu.Name】的详细信息</h1>
        <ul>
            <li>学号:@ViewBag.Stu.Id</li>
            <li>姓名:@ViewBag.Stu.Name</li>
            <li>性别:@ViewBag.Stu.Sex</li>
            <li>年龄:@ViewBag.Stu.Age</li>
        </ul>
    </div>
</body>

三、TempData对象

  1. TempData是一种字典对象,也能用于从“控制器到视图的数据传递”,和ViewData类似
  2. TempData还能实现“不同请求之间”的数据传递,跨请求数据传递
  3. TempData保存数据的机制是Session,但又不完全和Session相同
  4. TempData保存数据后,如果被使用,就会被清除,因此后面的请求将不能再次使用
  5. TempData保存数据后,如果没有被使用,则他保存的时间是Session的生命周期

四、View()+Model

在创建视图时选择模型类,即接口Model层自定义模型对象
控制器中:

		public ActionResult Deatil(int Id)
        {
            //根据Id获取对应的对象信息,因此需要从客户端的请求中获取数据
            Student student = server.GetStudentById(Id);
            if (student != null)
            {
                //控制器在返回视图的时候应该将数据模型作为视图参数传递给视图
                return View(student);
            }
            else
            {
                return View("Error");
            }
        }
        
@*//视图中使用一个变量model来接受数据模型,并转换数据模型的类型*@
@model Model.Student

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@(Model.Name) </title>
</head>
<body>
    <div> 
        <ul>
            <li>学号:@Model.Id</li>
            <li>姓名:@Model.Name</li>
            <li>性别:@Model.Sex</li>
            <li>年龄:@Model.Age</li>
        </ul>
    </div>
</body>
</html>

五、常用数据传递

传递方式 应用场合 跨请求
ViewData 适合传递单个数据,需要类型转换 不能
ViewBag 适合传递单个数据,不需要类型转换 不能
TempData 主要用来跨多个动作方法传递数据
View()+Model 适合传递模型数据,不需要类型转换
原创文章 155 获赞 325 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dust__/article/details/106111104