zoukankan      html  css  js  c++  java
  • MVC 从后台页面 取前台页面传递过来的值的几种取法

     
     

    <1>前台页面 Index视图

    注意:用户名表单的name值为txtName

               密码表单的name值为txtPassword

     1 <html>
     2 <head>
     3     <meta name="viewport" content="width=device-width" />
     4     <title>Test</title>
     5 </head>
     6 <body>
     7     <form action="/Home/Test" method="post">
     8         <div>
     9             <label>用户名</label><input type="text" name="txtName" />
    10             <label>密 码</label><input type="text" name="txtPassword" />
    11         </div>
    12         <input type="submit" value="提交" />
    13     </form>
    14 </body>
    15 </html>

     

    <2>后台页面,Home控制器 (为了测试,分别将视图页中的from表单的action设为 action="/Home/Test" ,action="/Home/Test2" action="/Home/Test3" action="/Home/Test4" )

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.Mvc;
     6 
     7 namespace MvcApplication1.Controllers
     8 {
     9     public class HomeController : Controller
    10     {
    11         //
    12         // GET: /Home/
    13 
    14         public ActionResult Index()
    15         {
    16             return View();
    17         }
    18 
    19         /// <summary>
    20         /// MVC第一种取值方式
    21         /// </summary>
    22         /// <returns></returns>
    23         public ActionResult Test()
    24         {
    25             string userName = Request["txtName"];   //此时Request["txtName"]=ABC
    26             string password = Request["password"];  //此时Request["password"]=123
    27 
    28             return Content("OK" + userName + password);
    29         }
    30         /// <summary>
    31         /// 第二种取值方式
    32         /// </summary>
    33         /// <param name="f"></param>
    34         /// <returns></returns>
    35         public ActionResult Test2(FormCollection f) //FormCollection是MVC中表单里的一个集合,它也可以来接收前台提交过来的表单,前台提交过来的表单全都封装到这个对象中来了
    36         {
    37             string userName = f["txtName"];     //此时f["txtName"]=ABC 
    38             string password = f["txtPassword"]; //此时f["txtPassword"]=123
    39 
    40             return Content("OK" + userName + password);
    41         }
    42         /// <summary>
    43         /// 第三种取值方式
    44         /// </summary>
    45         /// <param name="txtName"></param>
    46         /// <param name="txtPassword"></param>
    47         /// <returns></returns>
    48         public ActionResult Test3(string txtName, string txtPassword) //注意这个参数的名称必须要与前台页面控件的 name值是一致的
    49         {
    50             return Content("OK" + txtName + txtPassword);
    51 
    52             //此时textName=ABC 
    53             //此时txtPassword=123
    54         }
    55 
    56         /// <summary>
    57         /// 第四中方式
    58         /// </summary>
    59         /// <param name="txtName"></param>
    60         /// <param name="txtPassword"></param>
    61         /// <param name="p"></param>
    62         /// <returns></returns>
    63         public ActionResult Test4(string txtName, string txtPassword, ParaClass p) //如果ParaClass类里面的属性与前台页面控件的name值一致,那么它的对象属性也会自动被赋值
    64         {
    65             return Content("OK" + txtName + txtPassword + p.txtName + p.txtPassword);
    66 
    67             //此时textName=ABC 
    68             //此时txtPassword=123
    69 
    70             //此时p.txtName=ABC
    71             //此时p.txtPassword=123
    72         }
    73 
    74 
    75         public class ParaClass
    76         {
    77             public string txtName { get; set; } //此时textName=ABC
    78             public string txtPassword { get; set; } //此时txtPassword=123
    79         }
    80       
    81     }
    82 }
    
    
  • 相关阅读:
    idea添加类注释和方法注释
    蓝桥杯ALGO-1,区间k大数查询
    personalblog
    ul+li水平居中的几种方法
    前端ps部分
    帝国cms-tab
    帝国cms判断某一字段是否为空
    帝国cms建站总结-(分页)
    Js获取验证码倒计时
    前端截取字符串:JS截取字符串之substring、substr和slice详解
  • 原文地址:https://www.cnblogs.com/JerryTian/p/5079670.html
Copyright © 2011-2022 走看看