65.9K
CodeProject 正在变化。 阅读更多。
Home

从视图到控制器获取 POST 数据的不同方法

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.68/5 (32投票s)

2016 年 6 月 20 日

CPOL

2分钟阅读

viewsIcon

22386

在本文中,我们将了解通过哪些不同的方式,我们可以在控制器中接收视图的 post 数据。

引言

在 MVC 中,当我们需要在控制器的 action 方法中收集所有表单值时,可以通过几种方式在 Controller 中接收表单 post 数据。

不同的方法

我将介绍以下 4 种方法,通过这些方法,我可以在控制器中接收表单 Post 数据

  1. 强类型模型
  2. 请求对象
  3. 表单集合
  4. 通过参数

强类型模型

这是我们在 MVC 中使用的一种非常常见的方法。我们可以直接通过控制器中的模型接收表单值。 在这种方法中,我们将视图直接从模型绑定,并且当我们 post 表单时,我们可以在 post action 方法中,以该模型的对象作为参数接收所有表单数据。

示例:假设我的项目中有如下所示的 StudentModel

   public class StudentModel
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string PhoneNo { get; set; }
        public string Address { get; set; }
    }

我有一个名为 StudentController 的控制器,其中有一个 action 方法,名为 StudentRegistration,带有 HttpGet 动词,如下所示

  public class StudentController : Controller
    {
        [HttpGet]
        public ActionResult StudentRegistration()
        {
            return View();
        }
    }

右键单击 Action 方法,然后选择创建视图,并在创建视图时,选择强类型视图,如下所示

单击“添加”后,我们将在 Views/Student 文件夹中获得一个名为 StudentRegistration.cshtml 的视图。

现在,创建另一个带有 httpPost 动词的 action 方法,我们将在其中 post 我们的表单,如下所示

请求对象

假设我们不想使用强类型模型,并且我们在没有模型的帮助下创建视图。 假设我们如下创建 StudentRegistration 视图,那么我们将无法在模型对象中接收 post 数据。 在这种情况下,我们需要选择一种不同的方法来获取 action 方法中的 post 数据。

我们创建了一个名为 StudentReg 的 Action 方法,我们将在其中 post 表单,在那里,我们可以通过 Request 对象接收表单 post 数据,如下所示

 public class StudentController : Controller
    {
        [HttpGet]
        public ActionResult StudentRegistration()
        {
            return View();
        }

        [HttpPost]
        public ActionResult StudentReg()
        {
            string name = Request["txtName"].ToString();
            string email = Request["txtEmail"].ToString();
            string phoneNo = Request["txtPhoneNo"].ToString();
            string password = Request["txtPassword"].ToString();
            return View();
        }

    }

表单集合

这是在控制器中获取 post 数据的另一种方法。 我们也可以通过 Form 集合获取 post 数据。 假设我们有一个如上例所示的视图,并且想要在 Controller 中获取所有 post 数据,那么我们将使用 Form Collection,如下所示

 public class StudentController : Controller
    {
        [HttpGet]
        public ActionResult StudentRegistration()
        {
            return View();
        }
        [HttpPost]
        public ActionResult StudentReg(FormCollection col)
        {
            string name = col["txtName"].ToString();
            string email = col["txtEmail"].ToString();
            string phoneNo = col["txtPhoneNo"].ToString();
            string password = col["txtPassword"].ToString();
            return View();
        }
    }

因此,我们也可以通过 Form Collection 接收值,而且它也很容易。

通过参数

我们可以直接在 Action 方法的参数中接收 post 数据。 参数名称应与控件的 id 相同。 例如,如果视图上文本框的 Id 为 txtName,则参数名称应为 txtName。 这是一个例子

 public class StudentController : Controller
    {
        [HttpGet]
        public ActionResult StudentRegistration()
        {
            return View();
        }
        [HttpPost]
        public ActionResult StudentReg(string txtName,string txtEmail,
                                       string txtPhoneNo,string txtPassword)
        {
            return View();
        }
    }

StudentRegistration 视图中,我们有 NameEmailPhoneNoPassword 文本框,它们的 id 分别为 txtNametxtEmailtxtPhoneNotxtPassword

注意:控件 ID 应该相同,但我们可以按任何顺序传递参数,即,我们可以在 txtName 之前编写 txtEmail

最后 - 如果这有帮助并且你喜欢它,请投票。

© . All rights reserved.