(精华)2020年8月19日 ASP.NET MVC 视图传参的几种方式
发布日期:2021-06-29 15:10:20 浏览次数:3 分类:技术文章

本文共 3537 字,大约阅读时间需要 11 分钟。

/// /// 模拟一套数据/// private List
_UserList = new List
() {
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: Firstpublic 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"]);}

FirstIndex

ViewData["ViewDataCurrentUser"].Name:@(((CurrentUser)ViewData["ViewDataCurrentUser"]).Name)

ViewData["ViewDataCurrentUser"].Name:@(ViewDataCurrentUser.Name)

ViewData["testProp"]:@ViewData["testProp"]

ViewBag.Name:@ViewBag.Name

ViewBag.ViewBagCurrentUser.Name:@ViewBag.ViewBagCurrentUser.Name

TempData["TempDataCurrentUser"].Name:@(((CurrentUser)TempData["TempDataCurrentUser"]).Name)

TempData["TempDataCurrentUser"].Name:@(TempDataCurrentUser.Name)

TempData["testProp"]:@TempData["testProp"]

ViewBag.testProp:@ViewBag.testProp

Model.Name:@Model.Name

views 下面的web.config定义

/// 一个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未通过检测"); }}

转载地址:https://codeboy.blog.csdn.net/article/details/108093942 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:(精华)2020年8月19日 ASP.NET MVC 内置扩展控件的使用
下一篇:(精华)2020年8月19日 数据库设计 数据库优化(数据库自身的优化,数据库表优化,程序操作优化)

发表评论

最新留言

不错!
[***.144.177.141]2024年04月21日 17时04分19秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章