码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-01-29 16:27:15      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:class   对象   控制器   本质   cti   blog   article   content   tps   

https://blog.csdn.net/weixin_30379911/article/details/95065387

方法一:通过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();
        }

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

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

标签:class   对象   控制器   本质   cti   blog   article   content   tps   

原文地址:https://www.cnblogs.com/wfy680/p/12240284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!