zoukankan      html  css  js  c++  java
  • ASP.NET MVC学习——控制器传递数据到view的简单案例

    从控制传数据到view端的方法:
    方法一:修改Control数据,添加ViewBag
    1、ViewBag.+ 任意变量名 = "你想要输入的数据"

    2、在Control对应的cshtml中添加@ViewBag.+你的变量

    3、最后按地址运行

    方法二:修改Control数据,添加数据


    方法三:

            public ActionResult About()
            {
    
                TempData["message"] = "Hellow world!";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                string MessageContact = TempData["message"].ToString();
    
                ViewBag.message = MessageContact;
    
                return View();
            }
    

    Index()给TempData添加了一个键值对,假设我们先请求Index这个Action,接着请求Index2这个Action,那么在Index2中,我们便可以得到之前添加到TempData的键值对。有趣的是,这时如果再次请求Index2,那么从TempData中读到的MyName的值会是null。这是因为TempData和一个临时的Session差不多,当Acion执行的时候它做为一个全局对象保存的内存中,而一旦Action的执行完成,就会释放内存空间,这就是它与ViewData最大的不同之处。

    方法四:

            public ActionResult About()
            {   //不可以传递string类型
                return View(666667777);
            }
    
    <body>
     <p>@Model</p>
    </body>
    

    方法五:

            public ActionResult Index()
            {
    
                return Content(Request.QueryString["name"]);
            
            }
    

    同时view传递多个值给control

            public ActionResult Index()
            {
    
                return Content($"{ Request.QueryString["name"]}{ Request.QueryString["age"]}");
            
            }
    

    方法六:

    <form action="/Home/PostMessage" method="post">
        <input type="text" name="name" />
        <button type="submit">提交</button>
    </form>
    

    方法七:

            public ActionResult Datareback()
            {
                Response.Write(s: "hello world");
                return Content("");
            }
    

    方法八:重定位

            public ActionResult RedirectWeb()
            {
                Response.Redirect(url: "http://www.baidu.com");
                return Content("");
            }
    

  • 相关阅读:
    css3 transform语法
    自我规范(待增加)
    对于闭包的自我理解
    canvas教程没讲的细节
    react新手教程井字棋游戏
    div自动填满剩余部分(等高布局)
    记录自己以后可能会使用到的想法
    Python__list 常用操作
    Python基础语法(四)-- 函数
    Python基础语法(三)-- 循环
  • 原文地址:https://www.cnblogs.com/LY-CS/p/12719442.html
Copyright © 2011-2022 走看看