asp.net mvc 传参和视图赋值
传参分为:
1.路由传参
2.“问号?”传参
3.表单form传参
4.通过route特性路由传参
1.路由传参
视图传参代码:
<a href="chuanzhi_luyou/666666">路由传值</a>
控制器代码:
public ActionResult ChuanZhi_LuYou() { /* * =========【取值方法一(获取路由信息)】========= * RouteData.Values["id"] * RouteData.Values["变量"] 获取路由信息 * */ /* * ViewData["Request_id"] * ViewBag.Request_id (动态类型) * 向视图传递参数有ViewData["变量"]和ViewBag."变量"两种 * */ ViewBag.RouteData_Values_id = RouteData.Values["id"].ToString(); return View(); }
视图取参代码:
<h1>路由传值</h1> <h5>ViewBag.RouteData_Values_id=@ViewBag.RouteData_Values_id</h5>
2.“问号?”传参
视图传参代码:
<li><a href="chuanzhi_wenhao?id=666666">?传值</a></li>
控制器代码:
public ActionResult ChuanZhi_WenHao() { /* * =========【取值方法二(获取"?"问号传参)】========= * Request["id"].ToString(); * Request["变量"]取值方式可以获取get和post两种 * =========【取值方法三(获取"?"问号传参)】========= * Request.QueryString["id"] * */ /* * ViewData["Request_id"] * ViewBag.Request_id (动态类型) * 向视图传递参数有ViewData["变量"]和ViewBag."变量"两种 * */ ViewData["Request_id"] = Request["id"].ToString(); ViewBag.Request_id = Request["id"].ToString(); ViewData["Request_QueryString_id"] = Request.QueryString["id"]; ViewBag.Request_QueryString_id = Request.QueryString["id"]; return View(); }
视图取参代码:
<h1>?传值</h1> <h5>ViewData["Request_id"]=@ViewData["Request_id"]</h5> <h5>ViewBag.Request_id=@ViewBag.Request_id</h5> <h5>ViewData["Request_QueryString_id"]=@ViewData["Request_QueryString_id"]</h5> <h5>ViewBag.Request_QueryString_id=@ViewBag.Request_QueryString_id</h5>
3.表单form传参
视图传参代码:
<h1>form 传值</h1> <form action="chuanzhi_biaodan" method="post"> 用户名:<input type="text" name="Username"/> <br /> 密码:<input type="password" name="UserPassword"/> <br /> <input type="submit" value="登录" /> </form>
控制器代码:
public ActionResult ChuanZhi_BiaoDan() { /* * ViewData["Request_id"] * ViewBag.Request_id (动态类型) * 向视图传递参数有ViewData["变量"]和ViewBag."变量"两种 * */ ViewBag.UserName = Request["UserName"].ToString(); ViewBag.UserPassword= Request["UserPassword"].ToString(); return View(); }
视图取参代码:
<h1>form 传值</h1> <h5>ViewBag.UserName=@ViewBag.UserName</h5> <h5>ViewBag.UserPassword=@ViewBag.UserPassword</h5>
4.通过route特性路由传参
#region ==============通过route特性路由传参=============== /*【必须】启用路由特性映射 * MVC5可以通过路由配置文件(App_Start/RouteConfig.cs)增加下面代码 * routes.MapMvcAttributeRoutes(); */ /// <summary> /// 通过route特性路由传参[string] /// 访问:http://localhost:53676/ChuanZhi/c_string/xiaoming /// </summary> /// <returns></returns> [Route("ChuanZhi/c_string/{name}")] public string ceshi_string(string name) { string status = string.Empty; status = name; return status; } /// <summary> /// 通过route特性路由传参[int] 参数不可空 /// 访问:http://localhost:53676/ChuanZhi/c_int/1 /// </summary> /// <returns></returns> [Route("ChuanZhi/c_int/{proid:int}")] public string ceshi_int(int proid) { int sum=proid+proid; return $"{proid}+{proid}={sum}"; } /// <summary> /// 通过route特性路由传参[int] 参数可空 /// 访问:http://localhost:53676/ChuanZhi/c_int/1 /// </summary> /// <returns></returns> [Route("ChuanZhi/c_int2/{proid?}")] public string ceshi_int2(int proid=1) { int sum=proid+proid; return $"{proid}+{proid}={sum}"; } #endregion
视图赋值
向视图传递参数有ViewData["变量"]和ViewBag."变量"两种。
例如上面的ViewData["Request_id"]和ViewBag.Request_id,无论是双引号(“”)里的Request_id还是(.)后面的Request_id都是自定义的。最规范的命名方式是根据参数命名。
/*
* ViewData["Request_id"]
* ViewBag.Request_id (动态类型)
* 向视图传递参数有ViewData["变量"]和ViewBag."变量"两种
* */
————————————————
版权声明:本文为CSDN博主「橙-景安服务器代理」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cplvfx/article/details/100926846
qq:527592435