首页 技术 正文
技术 2022年11月16日
0 收藏 470 点赞 3,547 浏览 2570 个字

  

   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、隐藏域

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849