zoukankan      html  css  js  c++  java
  • MVC中页面传值方式总结

     

    MVC中的页面传值,通常指Controller和view之间的数据传递,经常用到的有几种方式,总结如下:

    一、Controller----------->View(控制器传到视图)

    1、ViewData

    获取或设置一个字典,其中包含在控制器和视图之间传递的数据。使用ViewData是采用键值对的形式,对所定义的数据进行传递。在View中会自动识别到拥有唯一键值的ViewData,并将数据显示出来。

    例子:

    public ActionResult() 
    {
    <span style="white-space:pre">	</span>List<Models.BlogArticle> list = (from d in db.BlogArticles where d.AIsDel == false select d).ToList();
                //2将集合数据传给视图
                // ViewBag.DataList = list;//viewBag形式
                //利用viewdata形式
                ViewData["DataList"] = list;
    
                return View();
    }

    视图中接收:

     <table id="tbList">
            <tr>
                <th>id</th>
                <th>标题</th>
                <th>分类</th>
                <th>状态</th>
                <th>时间</th>
                <th>操作</th>
            </tr>
    
    
            <!--遍历Action方法,设置给ViewData的集合数据,生成html代码-->
            @foreach (BlogArticle a in ViewData["DataList"] as List<BlogArticle>)
            {
                <tr>
                    <td>@a.AId </td>
                    <td>@a.ATitle </td>
                    <td>@a.ACate</td>
                    <td>@a.AStatu </td>
                    <td>@a.AUpdatetime </td>
                    <td>@a.AContent </td>
                    <td>
                        <a href="javascript:del(@a.AId)">删</a>
                        <a href="/home/modify/@a.AId">改</a>
                    </td>
                 </tr>
    
            }
        </table>


    2、ViewBag

    获取视图包,允许自定义属性进行赋值,属于动态类型(dynamic),以ViewBag.属性=属性值得方式进行传值,其实这里跟ViewData的使用原理类似。

    例子:

    public ActionResult Index()
    {
             ViewBag.Title="Hello!";
    }

    视图中接收

    <h1>ViewBag.Title</h1>


    3、其他方式

    在从controller向view传值时,除了以上两种方式外,还包括一些链接的方式,例如

    页面跳转的方式RedirectToAction,还有RenderAction等自动接收等方式。

    二、从View-------->Controller(视图传到控制器)

    1、其实在这种情况下,通常会选择利用ajax来通过get或者post进行提交。如果采用最原始的JS来做,就要用到之前总结的ajax经典的五步工作法了,但是通常我们采用JQuery封装好的ajax提交方式。

    即$.ajax({type,url,data,success:function(){})其中最常用的就是这几个参数属性了。

    2、通过Get方式+路由配置+id值进行提交数据

    <td>
                        <a href="javascript:del(@a.AId)">删</a></td>

    JS:

     <script type="text/javascript">
            function del(id) {
                if (confirm("您确定要删除吗?亲~~")) {
                    window.location = "/home/del/" + id;//通过get方式采用路由配置+id的形式
                }
            }
         </script>

    三、Action---------->Action

    前面两种传值方式都是在view和Controller之间进行数据传递,那么如果某一个业务需要用到后台的两个Action,并且需要再这两个Action之间进行数据传递,这时需要用到另一个概念TempData:获取要传递到视图的临时数据.使用时,需要注意TempData的生命周期,只在第一次请求Action时临时数据存在,之后自动变为NULL,具体的使用与ViewData相同,属于键值对的数据字典类。

    public ActionResult Index()
    	{
    		this.TempData["str"]="wyy";
    		return View();
    	}
    	public ActionResult Index2()
    	{
    		string name=this.TempData["str"];
    		return Content(name);
    	}

    以上是在学习和实践MVC过程中经常用到的页面传值的几种方式,大的方向看来从C向V数据传递以ViewData

    为基础,扩展到ViewBag,更加方便快速了。从V到C传递,则归于ajax中的get和post的提交了。

  • 相关阅读:
    POJ 1795 DNA Laboratory
    CodeForces 303B Rectangle Puzzle II
    HDU 2197 本源串
    HDU 5965 扫雷
    POJ 3099 Go Go Gorelians
    CodeForces 762D Maximum path
    CodeForces 731C Socks
    HDU 1231 最大连续子序列
    HDU 5650 so easy
    大话接口隐私与安全 转载
  • 原文地址:https://www.cnblogs.com/shenbing/p/5134333.html
Copyright © 2011-2022 走看看