.NET MVC5专题(前后端交互传参方式)

前后端的各种传参

private List<CurrentUser> _UserList = new List<CurrentUser>()
{
    new CurrentUser()
    {
        Id=1,
        Name="Z",
        Account="Administrator",
        Email="[email protected]",
        LoginTime=DateTime.Now,
        Password="123456"
    },
    new CurrentUser()
    {
        Id=2,
        Name="白天搬砖",
        Account="Administrator",
        Email="[email protected]",
        LoginTime=DateTime.Now,
        Password="123456"
    },
    new CurrentUser()
    {
        Id=3,
        Name="晚上做梦",
        Account="Administrator",
        Email="[email protected]",
        LoginTime=DateTime.Now,
        Password="123456"
    },
};

// GET: First
/// 
///  123  
/// 
/// 
/// 
public ActionResult Index(int id = 3)
{

    //base.HttpContext.Session
    //System.Web.Mvc.WebViewPage
    CurrentUser currentUser = this._UserList.FirstOrDefault(u => u.Id == id)
        ?? this._UserList[0];

    base.ViewData["CurrentUserViewData"] = this._UserList[0];
    base.ViewBag.CurrentUserViewBag = this._UserList[1];

    base.ViewData["TestProp"] = "cx";
    base.ViewBag.TestProp = "Tenk";
    base.TempData["TestProp"] = "Spider";//独立存储

    base.TempData["CurrentUserTempData"] = currentUser;

    if (id == 1 || id == 2 || id == 3)
        return View(this._UserList[2]);
    else if (id < 10)
        return View("~/Views/First/Index1.cshtml");
    else
        return base.RedirectToAction("TempDataShow");
}
//dynamic

public ActionResult TempDataShow()
{
    return View();
}


/// 
/// /First/IndexId/4 id是路由解析出来的,只有id参数可以这样
/// /First/IndexId?id=3 url地址传递参数
/// 
/// 
/// 
public ViewResultBase IndexId(int id)
{
    //各种的数据库增删改查
    return View();
}
public ViewResult IndexIdNull(int? id)
{
    return View();
}
/// 
/// string可以为空
/// /First/stringname?name=小白
/// 
/// 
/// 
public string StringName(string name)
{
    return $"This is {name}";
}

public string StringJson(string name)
{
    return Newtonsoft.Json.JsonConvert.SerializeObject(new
    {
        Id = 123,
        Name = name
    });
}

public JsonResult Json(int id, string name, string remark)
{
    return new JsonResult()
    {
        Data = new
        {
            Id = id,
            Name = name ?? "X",
            Remark = remark ?? "这里是默认的"
        },
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}

你可能感兴趣的:(.NET,MVC5专题)