首页 > 其他 > 详细

Action参数和ActionResult

时间:2018-07-26 10:38:17      阅读:273      评论:0      收藏:0      [点我收藏+]

一、Action

       1、Action参数: 普通参数、Model类、FormCollection

        (1)、普通参数 Index(string name,int age)   框架会自动把用户请求的QueryString 或者Post表单中的值根据参数名字映射对应参数的值,适用于查询参数比较少的情况。

        public ActionResult F3(string name, int age)
        {
            return Content("姓名:" + name + "年龄:" + age);
        }

        (2)、Model类: 这种类叫ViewModel

        public ActionResult Index(IndexModel model)
        {  
            return View(model);
        }

       (3)、 FormCollection ,采用fc["name"]这种方式访问,适用于表单元素不确定的情况,用的比较少。

        public ActionResult F2Show()
        {
            
            return View();
        }
        public ActionResult F2(FormCollection fc)
        {
            string name = fc["name"];
            string age = fc["age"];
            return Content("姓名:" + name + "年龄:" + age);
        }
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>F2Show</title>
</head>
<body>
    <form action="/Test/F2" method="post">
        <input type="text" name="name" />
        <input type="text" name="age" />
        <input type="submit" />
    </form>
</body>
</html>

         (4)  一部分是普通参数,一部分Model:

        public ActionResult Index(IndexModel model,string department)
        {  
            return Content(model.Num1+model.Num2+department);
        }

        (5) 添加默认值,默认值参数在最后

        public ActionResult F3(string name, int age=12)
        {
            return Content("姓名:" + name + "年龄:" + age);
        }

       2、 Action 方法不能重载,除了加上[HttpGet] 、[HttpPost] 标记

        [HttpGet]
        public ActionResult F4()
        {

            return View();
        }

        [HttpPost]
        public ActionResult F4(string name,int age)
        {

            return Content("姓名:" + name + "年龄:" + age);
        }
<body>
    <form action="~/Test/F4" method="post">
        <input type="text" name="name" />
        <input type="text" name="age" />
        <input type="submit" />
    </form>
</body>

Action参数和ActionResult

原文:https://www.cnblogs.com/fuyouchen/p/9368875.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!