介绍一些View中常用的东西
1:传递参数:
1):路由协议中传递参数:
1):eg:比如是这样类似的路由协议,那么我们在传递参数的时候,就要传递 id过去,当然如果,ABCD= UrlParameter.Optional ,那么我们传递参数的时候也要传递ABCD
光说不练都是假把式:
Global.asax:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
View:index.aspx
Controller:
1
///
<summary>
2 /// eg:ABCD="abcde...";
3 /// </summary>
4 /// <param name="ABCD"> 传递的参数名称是这样的才能获得值 </param>
5 /// <returns></returns>
6 public ActionResult About( string ABCD)
7 {
8 return View();
9 }
2 /// eg:ABCD="abcde...";
3 /// </summary>
4 /// <param name="ABCD"> 传递的参数名称是这样的才能获得值 </param>
5 /// <returns></returns>
6 public ActionResult About( string ABCD)
7 {
8 return View();
9 }
2):模型传值
Controller:index
1
using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace MVCTest.Controllers
8 {
9 [HandleError]
10 public class HomeController : Controller
11 {
12
13 public ActionResult Index()
14 {
15 ViewData[ " Message "] = " 欢迎使用 ASP.NET MVC! ";
16 return View();
17 }
18
19 /// <summary>
20 /// 当然强类型的也可以这样传值:
21 /// public ActionResult Index(string UserID ,string UserName,string Pwd)
22 /// </summary>
23 /// <returns></returns>
24 [HttpPost]
25 public ActionResult Index(UserName user)
26 {
27 ViewData[ " Message "] = " 欢迎使用 ASP.NET MVC! ";
28 return View();
29 }
30 }
31 public class UserName
32 {
33 public string UserID { get; set; }
34 public string UserName { get; set; }
35 public string Pwd { get; set; }
36 }
37 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace MVCTest.Controllers
8 {
9 [HandleError]
10 public class HomeController : Controller
11 {
12
13 public ActionResult Index()
14 {
15 ViewData[ " Message "] = " 欢迎使用 ASP.NET MVC! ";
16 return View();
17 }
18
19 /// <summary>
20 /// 当然强类型的也可以这样传值:
21 /// public ActionResult Index(string UserID ,string UserName,string Pwd)
22 /// </summary>
23 /// <returns></returns>
24 [HttpPost]
25 public ActionResult Index(UserName user)
26 {
27 ViewData[ " Message "] = " 欢迎使用 ASP.NET MVC! ";
28 return View();
29 }
30 }
31 public class UserName
32 {
33 public string UserID { get; set; }
34 public string UserName { get; set; }
35 public string Pwd { get; set; }
36 }
37 }
View:index.aspx
1 <%@ Page Language=
"
C#
" MasterPageFile=
"
~/Views/Shared/Site.Master
" Inherits=
"
System.Web.Mvc.ViewPage<MVCTest.Controllers.UserName>
" %>
2
3 <asp:Content ID= " Content1 " ContentPlaceHolderID= " TitleContent " runat= " server ">
4 主页
5 </asp:Content>
6 <asp:Content ID= " Content2 " ContentPlaceHolderID= " MainContent " runat= " server ">
7 <h2>
8 强类型测试</h2>
9 <% Html.BeginForm(); %>
10 <p>
11 用户ID:<%=Html.TextBoxFor(p=>p.UserID) %>
12 </p>
13 <p>
14 用户名称:<%=Html.TextBoxFor(p=>p.UserName) %>
15 </p>
16 <p>
17 用户密码:<%=Html.TextBoxFor(p=>p.Pwd) %>
18 </p>
19 <input type= " submit " value= " 提交 " />
20 <%--点击提交后, Index(UserName user)会接受到传递过来的参数--%>
21 <% Html.EndForm(); %>
22 </asp:Content>
2
3 <asp:Content ID= " Content1 " ContentPlaceHolderID= " TitleContent " runat= " server ">
4 主页
5 </asp:Content>
6 <asp:Content ID= " Content2 " ContentPlaceHolderID= " MainContent " runat= " server ">
7 <h2>
8 强类型测试</h2>
9 <% Html.BeginForm(); %>
10 <p>
11 用户ID:<%=Html.TextBoxFor(p=>p.UserID) %>
12 </p>
13 <p>
14 用户名称:<%=Html.TextBoxFor(p=>p.UserName) %>
15 </p>
16 <p>
17 用户密码:<%=Html.TextBoxFor(p=>p.Pwd) %>
18 </p>
19 <input type= " submit " value= " 提交 " />
20 <%--点击提交后, Index(UserName user)会接受到传递过来的参数--%>
21 <% Html.EndForm(); %>
22 </asp:Content>
3):ViewData与TempData的传值,TempData的有效期,从一个ActionResult到另外一个ActionResult就消失了