zoukankan      html  css  js  c++  java
  • ASP.NET MVC 数据传递 控制器向视图传递

    控制器向视图传递

    MVC 控制器向视图传递传递主要分为单页面传递和全局页面传递

    1.单页面传递主要是用 ViewData属性 和ViewBag属性
    语法:
    赋值: ViewData[“名称”]=数据;
    取值:在页面取值 @ViewData[“名称”] 来取值。

    案例说明

    HomeController类

     public ActionResult Index()
            {
                ViewData["Mess"] = "使用ViewData传递文本";
                ViewBag.Name = "张三";
                return View();
            }
    

    Index页面

    <h2>@ViewData["Mess"]</h2>
    <h3>ViewBag保存的姓名为:@ViewBag.Name</h3>
    

    展示
    在这里插入图片描述
    1.全局页面传递主要是用 TempData属性
    语法:
    赋值: TempData[“名称”]=数据;
    取值:在页面取值 @TempData[“名称”] 来取值。

    案例说明

    HomeController类

    public ActionResult Index()
            {
                TempData["mess1"] = "使用mess1传递文本";
                return View();
            }
    

    Index页面

    <a href="About">跳转到About</a>  @*跳转到About*@
    @*地址问题,要找同文件下的名命
            如:Index页面的地址是:https://localhost:44350/Home/Index
            如果要跳转到About页面需要把地址修改为: https://localhost:44350/Home/About
            只修改页面名称,不要拖进来地址不正确:
        <a href="~/Views/Home/About.cshtml">~/Views/Home/About.cshtml</a>
    *@
    

    About页面

    <h2 style="color:red">@TempData["mess1"]</h2>
    

    展示
    在这里插入图片描述
    点击 跳转到About按钮后
    在这里插入图片描述
    正确显示数据,要提一点的就是,在跳转昨天时要注意网页和你的文件夹位置。

  • 相关阅读:
    golang strings.Split函数
    Launch agent by connecting it to the master
    使用srvany.exe把程序安装成windows服务的方法
    区别对待 .gz 文件 和 .tar.gz 文件
    go 使用 sort 对切片进行排序
    Go数组遍历与排序
    Container killed on request. Exit code is 143
    ERROR tool.ImportTool
    报错笔记:sqoop 执行import命令报错
    连不上网
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13074405.html
Copyright © 2011-2022 走看看