zoukankan      html  css  js  c++  java
  • 论MVC中的传值

      

       2个页面分别为Father.cshtml、Child.cshtml

       2个控制器分别为FatherController.cs、ChildController.cs

       1个js,为Father.js

    一、FatherController传值给Father.cshtml:

      1.ViewBag 和 ViewData

        传递:   ViewBag.param1 = "hello";    // ViewData["param1"] = "hello";

        接收:  @ViewBag.param1   //@ViewData["param1"] = "hello"

        ViewBag 不再是字典的键值对结构,而是 dynamic 类型,它会在程序运行的时候动态解析。

        ViewBag和ViewData互通,仅针对当前Action中有效,生命周期和View相同。

        ViewBag可以直接被foreach,ViewData不行。

      2.TempData

        传递:   TempData["param1"]="hello";

        接收:   @TempData["param1]

        TempData至多只能经过一次Controller,并且每个元素至多只能被访问一次,之后就会被删除。

             本质是存在Session中。

             一般用于缓存或者抛出错误提示。

      3.Model

        传递: Person entity = new Person()  //事先定义好Person类

           {

            Name="jerry",Age=18;

           }

           return View(entity);  //如果返回视图名字就 return View("Index",entity);

        接收:  @Model.Name   @Model.Age

      

    二、Father.cshtml传值给FatherController:

      1.利用参数

        传递: <form name="form1" method="post" action="/Money/Father">  //method='get'/'post' 都行

              <input type="text" name="name" />
              <input type="tel" name="tel" />
              <input type="submit" />
                 </form>

        接收: public ActionResult Father(string name,string tel)  //无论get还是post都行
                 {
                      Response.Write("<script>alert('"+name + tel+"')</script>");
                      return View("Father");
                 }

      2.利用Request

        传递: <form name="form1" method="post" action="/Money/Father">  //method='get'/'post' 对应QueryString或Form

              <input type="text" name="name" />
              <input type="tel" name="tel" />
              <input type="submit" />
                 </form>

        接收: public ActionResult Father()  //get post 对应QueryString或Form
                 {

               string name=Request.QueryString["name"]/Form["name"];

               string tel=Request.QueryString["tel"]/Form["tel"];
                      Response.Write("<script>alert('"+name + tel+"')</script>");
                      return View("Father");
                 }

      3.设置Rounte

        首先在RouteConfig中添加:

    routes.MapRoute(
                    name: "myRoute1", //名字任意
                    url: "Money/Index/{a}/{b}/{c}",  //对指定的页面规定三个参数abc
                    defaults: new { controller = "Money", action = "Index",a = UrlParameter.Optional, b = UrlParameter.Optional, c = UrlParameter.Optional } //设置默认值
                );

        传递:   让当前url变成 xxx/Money/Index/1/2/3 即可  //其中xxx代表协议、域名、端口号

        接收:   public ActionResult Father(string a,string b,string c)  //这样定义好就可以直接使用了,值得注意的是这里参数名必须和routes中设置的参数名相同
                 {

              Response.Write("<script>alert('"+ a + b + c +"')</script>");

              return View("Father");

           }

    三、Father.cshtml传值给ChildController:

      同二,只是把路径改成ChildController。

    四、Father.js传值给FatherController:

      1、通过get

      2、通过post

      3、隐藏域

    五、FatherController传值给Father.js:

      1、Response.Write("<script type='text/javascript'>var name=" + Name + ";</script>");

      2、隐藏域

  • 相关阅读:
    添加绝对路径的链接
    css-------------控制溢出隐藏 换行用省略号表示
    引入公共头部 脚部
    入口图片放在浏览器正中间,点击之后缩小固定在浏览器一侧
    伪类 统一添加样式
    nav 鼠标移入当前高亮显示,其他消失
    17/9/6 bootstrap.css去掉后引发的样式错乱
    JavaScript的常见兼容问题及相关解决方法(chrome/IE/firefox)
    javascript特效实现——当前时间和倒计时效果
    使用DataTables导出excel表格
  • 原文地址:https://www.cnblogs.com/dengshaojun/p/4059893.html
Copyright © 2011-2022 走看看