zoukankan      html  css  js  c++  java
  • ASP.NET MVC 后台获取前台页面传值的几种方法和java获取参数方式类似

    ASP.NET MVC 后台获取前台页面传值的几种方法

    <1>前台代码(调用后台中指定的Action需更将前台中的Url改为指定的Action名

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

    复制代码
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.Mvc;  
      
    namespace MvcApplication1.Controllers  
    {  
        public class HomeController : Controller  
        {  
            //  
            // GET: /Home/  
      
            public ActionResult Index()  
            {  
                return View();  
            }  
      
      
            // MVC第一种取值方式  
      
            public ActionResult Test()  
            {  
                string userName = Request["txtName"];   //此时Request["txtName"]=ABC  
                string password = Request["password"];  //此时Request["password"]=123  
      
                return Content("OK" + userName + password);  
            }  
      
            // 第二种取值方式  
      
            public ActionResult Test2(FormCollection f) //FormCollection是MVC中表单里的一个集合,它也可以来接收前台提交过来的表单,前台提交过来的表单全都封装到这个对象中来了  
            {  
                string userName = f["txtName"];     //此时f["txtName"]=ABC   
                string password = f["txtPassword"]; //此时f["txtPassword"]=123  
      
                return Content("OK" + userName + password);  
            }  
      
            // 第三种取值方式  
      
            public ActionResult Test3(string txtName, string txtPassword) //注意这个参数的名称必须要与前台页面控件的 name值是一致的  
            {  
                return Content("OK" + txtName + txtPassword);  
      
                //此时textName=ABC   
                //此时txtPassword=123  
            }  
      
      
            // 第四种取值方式,通过类对象接收  
      
            public ActionResult Test4(ParaClass p) //如果ParaClass类里面的属性与前台页面控件的name值一致,那么它的对象属性也会自动被赋值  
            {  
                return Content("OK" + p.txtName + p.txtPassword);  
      
                //此时p.txtName=ABC  
                //此时p.txtPassword=123  
            }  
      
      
            public class ParaClass  
            {  
                public string txtName { getset; } //此时textName=ABC  
                public string txtPassword { getset; } //此时txtPassword=123  
      
                //注意:ParaClass类的属性名称必须要与前端页面控件的name值一致,这样才能接收到前端页面传递过来的值  
            }  
            
        }  
    }  
    复制代码

    原文链接:https://www.cnblogs.com/putongjiamao/p/7455308.html

  • 相关阅读:
    python3 接口测试 一般方法
    python2 接口测试一般方法.
    Postman 官网教程,重点内容,翻译笔记,
    Unity3D --对撞机/碰撞器 介绍
    MyBatis之配置文件与操作
    Eclipse连接mysql数据库jdbc下载(图文)
    MyBatis概述和环境的搭建
    样式加载不出来,浏览器控制台报错:Resource interpreted as Stylesheet but transferred with MIME type text/html
    bootstrap之navbar
    JSP中的内置标记(JSP动作)和 EL表达式
  • 原文地址:https://www.cnblogs.com/sunny3158/p/15298396.html
Copyright © 2011-2022 走看看