MVC中url传值和form传值

来源:互联网 发布:科技有限公司 软件开发 编辑:程序博客网 时间:2024/05/21 12:44

客户端的form 一般有两个提交方式 为 GET 和 POST 不常用的有PUT 和DELETE

<form action="/home/index?querystring_d=4" method="post">    <input type="text" name="input_a" id="input_a" value="1a" />    <input type="text" name="input_a" value="10a" />    <input type="text" name="input_b"value="2" />    <input type="text" name="input_c" value="3" />    <input type="text" name="inputlist_s" value="l1" />    <input type="text" name="inputlist_s" value="l2" />    <input type="text" name="inputlist_int" value="3a" />    <input type="text" name="inputlist_int" value="4" />    <input type="submit" value="queding" /></form>

GET:此方法会把action内,问号后的参数先删除,然后吧 form内的参数放到url中去传输.
POST:会把url内的参数发送,同时在content里发送form数据

1.Controller内action中的参数 支持post/get/?参数=1
接收form内数据
接收url内的数据

1传入数据不重复         public ActionResult Index(int input_a){return View()}         //数据直接获取,但格式不对应的话会为出错2传入数据重复,比如form内有两个相同name的input            public ActionResult Index(int input_a){return View()}        //只取第一个获取的数据,如果过传入数据格式不正确则值为null (form内有两个或多个相同name的input)        public ActionResult Index(int[] inputlist_int){return View()}        //如果传入数据格式只要有一个不正确 inputlist_int就为null 即使里面有对的数据

2.Request.Querystring[“val”]数据 支持get/?参数=1
-不接收form内数据-
接收url内的数据

1传入数据不重复        public ActionResult Index(){        {            string  input_a_1= Request.QueryString["input_a"];            return View()        }        //传入数据为string只取第一个获取的数据2传入数据重复,比如form内有两个相同name的input              public ActionResult Index(){            string  input_a_1= Request.QueryString["input_a"];            return View()        }        //传入数据为string 格式为"1,2,3",值为相同name的值合并并用逗号分割

3.在action中传入FormCollection数据 支持post
接收form内数据
-不接收url内的数据-

1传入数据不重复        public ActionResult Index(FormCollection form){        {             string input_a_form = form["input_a"];            return View()        }        //传入数据为string只取第一个获取的数据,同Request.Querystring2传入数据重复,比如form内有两个相同name的input              public ActionResult Index(FormCollection form){             string input_a_form = form["input_a"];            return View()        }        //传入数据为string 格式为"1,2,3",值为相同name的值合并并用逗号分割,同Request.Querystring

4.定义一个Model,在action的参数中使用model传入数据 支持post/get/?参数=1
接收form内数据
接收url内的数据

    public class Mymodel {//model必须为public 它的属性也必须为public       public string input_a { get; set; }       public string input_b { get; set; }       public string input_c { get; set; }       public int[] inputlist_int { get; set; }       public string querystring_d { get; set; }    }
1传入数据不重复        public ActionResult Index(Mymodel mymodel){return View()}        //可以传入通过url和from传入数据2传入数据重复,比如form内有两个相同name的input              public ActionResult Index(Mymodel mymodel){return View()}        //参数如果不是数组的话,那么相同name的input只会传入第一个name的input值,其他被丢弃        //传入的数据是数组的话 [] 的话那么重复值会直接传到这个入组里入上面的public int[] inputlist_int { get; set; }        //如果发现相同name里存在一个 "字符串"的话 其他的为 数字的话,那么存在一个一个错误格式,那么inputlist_int的值为null.

参数法 model法 querystring法 formcollection法 四中方法那种最好???

0 0