zoukankan      html  css  js  c++  java
  • Asp.Net.Core 5 传值 (控制器->界面)

    传值5种方式

    1,ViewBag

    2,ViewData

    3,TempData

    4,Model

    5,Session

    控制器代码

     public IActionResult Index()
            {
                base.ViewBag.UserName = "李潇潇";
                base.ViewData["UserName"] = "王萌萌";
                base.TempData["UerName"] = "彭晓晓";
    
                User user = new() { Id = 1, Name = "吴思思", Email = "123@163.com" };
                return View(user);
            }

    Index.cshtml代码

    @model AspNetCore5Demo.Models.User
    @{
        ViewData["Title"] = "Home Page";
    }
    
    <div class="text-center">
        <h1>ViewBag传值</h1>
        <h2>UserName:@ViewBag.UserName</h2>
        <h1>ViewData传值</h1>
        <h2>UserName:@ViewData["UserName"]</h2>
        <h1>TempData传值</h1>
        <h2>UserName:@TempData["UerName"]</h2>
    
        <hr />
        <h1>Model传值</h1>
        <h2>Id:@Model.Id</h2>
        <h2>Name:@Model.Name</h2>
        <h2>Email:@Model.Email</h2>
    </div>

    界面显示效果

    Session传值

    1,Startup.cs文件中增加配置

    方法-ConfigureServices 添加代码

     public void ConfigureServices(IServiceCollection services)
            {
                services.AddSession();//添加Session
                services.AddControllersWithViews();
            }

    2,方法-Configure 添加代码

     app.UseSession();

    3,控制器代码

        a,需要添加引用:using Microsoft.AspNetCore.Http;

        b,获取值:HttpContext.Session.GetString("UserNameSession");

        c,设置值:HttpContext.Session.SetString("UserNameSession", "刘晴晴");

     public IActionResult Index()
            {
                base.ViewBag.UserName = "李潇潇";
                base.ViewData["UserName"] = "王萌萌";
                base.TempData["UerName"] = "彭晓晓";
                User user = new() { Id = 1, Name = "吴思思", Email = "123@163.com" };
    
                //获取Session值,判断若为null则设置Session值
               var sUserName= HttpContext.Session.GetString("UserNameSession");
                if (sUserName == null)
                    HttpContext.Session.SetString("UserNameSession", "刘晴晴");
    
                return View(user);
            }

    4,index.cshtml代码

          a,引入 @using Microsoft.AspNetCore.Http

          b,获取值  @Context.Session.GetString("UserNameSession")

    @model AspNetCore5Demo.Models.User
    @using Microsoft.AspNetCore.Http
    @{
        ViewData["Title"] = "Home Page";
    }
    
    <div class="text-center">
        <h1>ViewBag传值</h1>
        <h2>UserName:@ViewBag.UserName</h2>
        <h1>ViewData传值</h1>
        <h2>UserName:@ViewData["UserName"]</h2>
        <h1>TempData传值</h1>
        <h2>UserName:@TempData["UerName"]</h2>
    
        <hr />
        <h1>Model传值</h1>
        <h2>Id:@Model.Id</h2>
        <h2>Name:@Model.Name</h2>
        <h2>Email:@Model.Email</h2>
    
        <!--获取Session值-->
        <h1>Session</h1>
        <h2>
            @Context.Session.GetString("UserNameSession")
        </h2>
    </div>
  • 相关阅读:
    Python学习 之 文件
    Python学习 之 对内存的使用(浅拷贝和深拷贝)
    Python学习 之 爬虫
    Python学习 之 正则表达式
    为何现在的网页广告都是有关你搜索或者购买过的商品 2015-08-22 22:06 1534人阅读 评论(35) 收藏
    Junit使用注意点
    用递归方式在JSON中查找对象
    利用StringBuffer来替换内容
    使用ant时 出现 java.lang.OutOfMemoryErro r: Java heap space的解决办法
    python-re使用举例
  • 原文地址:https://www.cnblogs.com/Linc2010/p/14313094.html
Copyright © 2011-2022 走看看