(精华)2020年8月19日 ASP.NET MVC 视图传参的几种方式

/// <summary>
/// 模拟一套数据
/// </summary>
private List<CurrentUser> _UserList = new List<CurrentUser>()
    { 
 
  
        new CurrentUser()
        { 
 
  
            Id=1,
            Name="人工智能1",
            Account="Administrator",
            Email="57265177@qq.com",
            LoginTime=DateTime.Now,
            Password="123456"
        },
        new CurrentUser()
        { 
 
  
            Id=2,
            Name="人工智能2",
            Account="Administrator",
            Email="57265177@qq.com",
            LoginTime=DateTime.Now,
            Password="123456"
        },
        new CurrentUser()
        { 
 
  
            Id=3,
            Name="人工智能3",
            Account="Administrator",
            Email="57265177@qq.com",
            LoginTime=DateTime.Now,
            Password="123456"
        },
         new CurrentUser()
        { 
 
  
            Id=4,
            Name="人工智能4",
            Account="Administrator",
            Email="57265177@qq.com",
            LoginTime=DateTime.Now,
            Password="123456"
        }
    };
// GET: First
public ActionResult Index(int i)
{ 
 
  

    logger.Info($"调用Index Action 参数为:{i}");

    base.ViewData["ViewDataCurrentUser"] = _UserList[0];
    base.ViewData["testProp"] = "jeff";

    base.ViewBag.testProp = "判断ViewBag.testProp 有没有覆盖ViewData[testProp]";  //会和ViewData["testProp"] 冲突,之后者为准;

    base.ViewBag.Name = "aaaaaaaaaaaaaaaaaaaaaaaaa";
    base.ViewBag.ViewBagCurrentUser = this._UserList[1];
    base.TempData["TempDataCurrentUser"] = this._UserList[2];
    base.TempData["testProp"] = "fresh";  //TempaData 存储在Sesssion中; 能够跨Action传递值
    if (i == 1)
    { 
 
  
        CurrentUser currentUser = this._UserList[3];
        return View(currentUser);
    }
    else
    { 
 
  
        return RedirectToAction("Index1");
    }
}
@using Advanced.AspNetMVC.Models
@model CurrentUser
@{ 
 
  
    ViewBag.Title = "FirstIndex";
    CurrentUser ViewDataCurrentUser = ((CurrentUser)ViewData["ViewDataCurrentUser"]);
    CurrentUser TempDataCurrentUser = ((CurrentUser)TempData["TempDataCurrentUser"]);
}
 

<h2>FirstIndex</h2>

<h3>ViewData["ViewDataCurrentUser"].Name:@(((CurrentUser)ViewData["ViewDataCurrentUser"]).Name)</h3>

<h3>ViewData["ViewDataCurrentUser"].Name:@(ViewDataCurrentUser.Name)</h3>

<h3>ViewData["testProp"]:@ViewData["testProp"]</h3>

<h3>ViewBag.Name:@ViewBag.Name</h3>

<h3>ViewBag.ViewBagCurrentUser.Name:@ViewBag.ViewBagCurrentUser.Name</h3>

<h3>TempData["TempDataCurrentUser"].Name:@(((CurrentUser)TempData["TempDataCurrentUser"]).Name)</h3>

<h3>TempData["TempDataCurrentUser"].Name:@(TempDataCurrentUser.Name)</h3>

<h3>TempData["testProp"]:@TempData["testProp"]</h3>

<h3> ViewBag.testProp:@ViewBag.testProp</h3>

<h3>Model.Name:@Model.Name</h3>

views 下面的web.config定义

<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <!--cshtml的父类,也能够扩展-->
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>
/// 一个HttpGet 一次HttpPost
/// MVC怎么识别呢?不能依赖于参数识别(参数来源太多不稳定)
/// 必须经过HttpVerbs来识别,
/// 若是没有标记,那么就用方法名称来识别
/// [ChildActionOnly] 用来指定该Action不能被单独请求,只能是子请求
/// [Bind]指定只从前端接收哪些字段,其余的不要,防止数据的额外提交
/// [ValidateAntiForgeryToken] 防重复提交,在cookie里加上一个key,提交的时候先校验这个
[AcceptVerbs(HttpVerbs.Put | HttpVerbs.Post)] //[HttpPost]
//[HttpPost]
//[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ProductId, CategoryId, Title, Price, Url, ImageUrl")]JDCommodity commodity)
{ 
 
  
    string title1 = this.HttpContext.Request.Params["title"];
    string title2 = this.HttpContext.Request.QueryString["title"];
    string title3 = this.HttpContext.Request.Form["title"];
    if (ModelState.IsValid)//数据校验
    { 
 
  
        JDCommodity newCommodity = this._iCommodityService.Insert(commodity);
        return RedirectToAction("Index");
    }
    else
    { 
 
  
        throw new Exception("ModelState未经过检测");
    }
}