MVC 传参

介绍一些View中常用的东西

1:传递参数:

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

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

                 光说不练都是假把式:

 Global.asax:                

View Code

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 {  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>
复制代码

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

转载于:https://www.cnblogs.com/DJYBlog/p/3396740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值