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

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

  • 相关阅读:
    redis启动
    supervisor thinkphp6 qune
    iview table header cell tooltip; iview表格 表头、单元格文字提示 ;iview 表格 悬浮提示
    .net mvc 中引入 echarts dataTool prepareBoxplotData
    iview table 初始化为null问题
    sqlserver 视图 EF无法从数据更新模型
    sql 空格变问号;sql 无法 去掉 空格 ;sql rtrim 失效;(转载)
    MongoDB的备份与恢复(转载)
    jQuery的deferred对象详解(转载)
    今天,我们小公司的服务器被黑了!!!
  • 原文地址:https://www.cnblogs.com/liangjie/p/2416933.html
Copyright © 2011-2022 走看看