饼子天空  
个人程序Blogs

介绍一些View中常用的东西

1:传递参数:

    1):路由协议中传递参数:

           1):eg:比如是这样类似的路由协议,那么我们在传递参数的时候,就要传递 id过去,当然如果,ABCD= UrlParameter.Optional ,那么我们传递参数的时候也要传递ABCD

                 光说不练都是假把式:

 Global.asax:                

View Code
1             routes.MapRoute(
2                 "Default"// 路由名称
3                 "{controller}/{action}.html"// 带有参数的 URL
4                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
5             );

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):模型传值

     

 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 { getset; }
34         public string UserName { getset; }
35         public string Pwd { getset; }
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>

   3):ViewData与TempData的传值,TempData的有效期,从一个ActionResult到另外一个ActionResult就消失了

 

posted on 2011-11-04 10:56  饼子天空  阅读(6084)  评论(0编辑  收藏  举报