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("");
        }

posted @ 2020-04-17 13:46  LY-CS  阅读(99)  评论(0编辑  收藏  举报