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();
            //}
        }

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

  • 相关阅读:
    C macro : the " do { ... } while(0)" magic
    sscanf()函数
    poj-1200-hash-
    hduoj-1735 简单的贪心算法
    hduoj -2570-简单的贪心算法入门
    分治算法应用-最近点对的最小距离-hdu 1007 Quoit Design
    分治算法(转载)
    快速幂总结
    poj 1065 贪心算法
    toj ~3988~递归二叉树三种遍历的转换
  • 原文地址:https://www.cnblogs.com/liangjie/p/2416933.html
Copyright © 2011-2022 走看看