zoukankan      html  css  js  c++  java
  • MVC使用基础——前后端交换数据

    1 获取参数

    Request.Form:获取以POST方式提交的数据(接收Form提交来的数据);   var data = Request.Form["data"];

    Request.QueryString:获取地址栏参数(以GET方式提交的数据)         var data = Request.QueryString["data"];

    2 页面跳转

    public actionResult Index

    {
    return viewn()  --返回默认视图Index.cshtml
    return View("PayList")  --从Index控制器调用PayList.cshtml  注:此处仅仅是调用视图,并未经过PayList控制器,如果控制器中有逻辑代码则就调用不到了
    return Redirct("Pay/PayList"); --只能通过url路径跳转(无重载)
    return RedirctToAction("PayList")  --从Index控制器调用PayList控制器,最后返回PayList.cshtml视图
    }
     
    public actionResult PayList
    {
    return View();  --返回默认视图PayList.cshtml视图
    }
     
    3 将后台数据上传到前端 之 使用   (ViewData与ViewBag)
     
    例如:

    public ActionResult Index()
    {
    List<string>colors = new List<string>();
    colors.Add("red");
    colors.Add("green");
    colors.Add("blue");
    ViewBag.ListColors = colors; //colors is List
    ViewBag.DateNow= DateTime.Now;
    ViewBag.Name= "hejingyuan";
    ViewBag.Age = 25;
    return View();
    }

    View


    <p>
    My name is <b>@ViewBag.Name</b>, <b>@ViewBag.Age</b> years old.
    <br />
    I like the following colors:
    </p>
    <ul id="colors">
    @foreach (var color in ViewBag.ListColors)
    {
    <li><font color="@color">@color</font> </li>
    }
    </ul>
    <p>
    @ViewBag.DateNow
    </p>


    4 base.TempData[""]   保存在session里面  所有的控制器都能使用    但是用过一次,就清空了

     
     
     
     
     
  • 相关阅读:
    rest framework Genericview
    rest framework Views
    rest framework Response
    rest framework Request
    C# Unity 依赖注入
    C# 缓存
    使用 Log4Net 做日志
    ORM 与 数据持久化
    Mycat 配置 笔记
    .NET自我进阶以及第一个框架搭建(二)
  • 原文地址:https://www.cnblogs.com/yyl001/p/10286095.html
Copyright © 2011-2022 走看看