(精华)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未通过检测");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愚公搬代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值