ASP.NET MVC 在控制器中接收视图表单POST过来的数据方法

方法一:通过Request.Form

        [HttpPost]
        public ActionResult Test()
        {
            string id=Request.Form["id"];

            return View();
        }

方法二:通过映射到FormCollection

        [HttpPost]
        public ActionResult Test(FormCollection form)
        {
            string id = form["id"];

            return View();
        }

方法三:通过映射到控制器方法参数

        [HttpPost]
        public ActionResult Test(string id)
        {
            //id是获取来自View表单POST过来的控件名为id的值

            return View();
        }

方法四:通过映射到视图数据对象

        [HttpPost]
        public ActionResult Test(TModel model)
        {
            string id = model.id;

            return View();
        }

方法五:通过调用UpdateModel方法

        [HttpPost]
        public ActionResult Test()
        {
            TModel model;
            UpdateModel<TModel>(model);

            return View();
        }

以上五种方法都可以获取视图中的值,其中方法一与方法二本质上是相同的,方法四与方法五本质上也是相同的,具体使用哪种方法依据实际情况来选择使用,建议若是获取指定的某一个或某几个的值,可使用方法一或方法三,若是获取整个表单所有的值,则可使用方法四方法五。

 同步发表于我的个人网站:http://www.zuowenjun.cn/post/2014/10/22/63.html

posted @ 2014-10-22 15:31  梦在旅途  阅读(2740)  评论(0编辑  收藏  举报