如何在控制器中获取文本框的值

时间:2014-06-11 08:37:59

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我想在视图中获取文本框中的值到asp.net_mvc中的控制器。不使用剃刀代码。我想通过c#代码获取值。我怎么去?

4 个答案:

答案 0 :(得分:1)

一个简单的选择是在控制器actionresult中有一个参数,它接收与你要发布的输入元素的id匹配的表单帖子

在你看来

<input type="text" name="MyField" id="MyField"/>

在您的控制器中

[HttpPost]
public ActionResult ReceivePost(string MyField)

答案 1 :(得分:1)

您可以使用HTML表单或razor表单,在帖子中可以使用FormCollection 像这样

[HttpPost]
public ActionResult myPost(FormCollection myform)
{
// You can access all the elements of the form from that object
}

关于此blog

的详细信息

答案 2 :(得分:1)

你可以试试这个:

<input type="text" name="txtname" id="txtname"/>

[HttpPost]
public ActionResult Action_name(FormCollection form)
{
 string val=form["txtname"];
 return View();
}

答案 3 :(得分:0)

创建一个html表单并将textboxinput命名为方法变量:

<form action="controllername/actionname">
    <input name="foo" id="foo" type="text"/>
    <input type="submit"/>
 </form>

你的mvc控制器:

[HttpPost]
public ActionResult actionname(string foo)
{
   //do stuff
}

MVC的模型绑定将命名输入绑定到方法的变量。还有其他方法,比如检索FormCollection等。

相关问题