zoukankan      html  css  js  c++  java
  • View传参数到Controller(asp.net mvc3) 中庸

         最近在用mvc3做项目,常走一些弯路,在此记录View传参数到Controller中的Action,Action接收参数的四种方式

           1.示例model

    public class testModel
        {
            public String A { get; set; }

            public String B { get; set; }

            public String C { get; set; }

            public String D { get; set; }
        }

            2.示例View

    @{
        ViewBag.Title = "test1";
    }

    @model MvcApplication2.Models.testModel

    <h2>test1</h2>
    @using (Html.BeginForm())
    {
    <table>
     <tr>
       <td><input type="text" name="A" /></td>
       <td><input type="text" name="B" /></td>
       <td><input type="text" name="C" /></td>
       <td><input type="text" name="D" /></td>
     </tr>
     <tr>
        <td colspan="4"><input type="submit" value="提交" /></td>
     </tr>
    </table>
    }

    3.示例Controller

    public class TestController : Controller
        {
       
            /// <summary>
            /// 视图传递参数到控制器中的Aciton的第四种方式 form post提交 model参数接收
            /// </summary>
            /// <param name="t"></param>
            /// <returns></returns>
            public ActionResult test1(testModel t)
            {  
                String a = t.A;
                String b = t.B;
                String c = t.C;
                String d = t.D;
                return View();
            }


            ///// <summary>
            ///// 视图传递参数到控制器中的Aciton的第三种方式 form提交 方法参数接收
            ///// </summary>
            ///// <param name="A"></param>
            ///// <param name="B"></param>
            ///// <param name="C"></param>
            ///// <param name="D"></param>
            ///// <returns></returns>
            //public ActionResult test1(String A,String B,String C,String D)
            //{
            //    String a = A;
            //    String b = B;
            //    String c = C;
            //    String d = D;
            //    return View();
            //}

            ///// <summary>
            ///// 视图传递参数到控制器中的Aciton的第二种方式 form post提交 request.form接收
            ///// </summary>
            ///// <returns></returns>
            //public ActionResult test1()
            //{
            //    String a = Request.Form["A"];
            //    String b = Request.Form["B"];
            //    String c = Request.Form["C"];
            //    String d = Request.Form["D"];
            //    return View();
            //}

            ///// <summary>
            ///// 视图传递参数到控制器中的Aciton的第一种方式 form post提交 FormCollection接收
            ///// </summary>
            ///// <param name="frm"></param>
            ///// <returns></returns>
            //public ActionResult test1(FormCollection frm)
            //{
            //    String a = frm["A"];
            //    String b = frm["B"];
            //    String c = frm["C"];
            //    String d = frm["D"];
            //    return View();
            //}
        }

     注:本人推荐使用第四方法,进行接收传过来的参数

  • 相关阅读:
    090828 H 小道理
    091031 T PowerShell Solution
    关注我们共有的家园,别让企鹅成为传说
    发布网站时自动切换connectionString
    bookmark: partitioned tables in sql server 2005
    it did suprise me a little bit..
    无法删除附加到事件上的匿名代理
    如何在自己工程项目中使用TouchJSON框架
    VMware 8安装苹果操作系统Mac OS X 10.7 Lion正式版
    Net线程间通信的异步机制
  • 原文地址:https://www.cnblogs.com/liangjie/p/2416933.html
Copyright © 2011-2022 走看看