使用ASP MVC </form>将字符串从<form>传递给控制器

时间:2011-12-18 20:38:36

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

我正在做这个小教程:http://www.asp.net/mvc/videos/mvc-2/how-do-i/creating-a-tasklist-application-with-aspnet-mvc

但不知怎的,他正在将一个字符串传回控制器。但我不能让我的传回一个字符串。我错过了什么?

Create.aspx

<form method="post" action="/Home/CreateNew">
    <label for="task">Task:</label>
    <input type="text" name="task" />
    <input type="submit" value="Add Task" /> 
</form>

HomeController.cs

public ActionResult CreateNew(object obj ) // <-- expecting a string but getting an object.
{
  string whattype = obj.GetType().ToString(); //just an obj, expecting a string
  //add to DB next
}

1 个答案:

答案 0 :(得分:4)

MVC是基于约定的 - 表单元素名称是task,因此应该是参数名称:

public ActionResult CreateNew(string task ) //<-- expecting a string but getting an object.
{
  string whattype = obj.GetType().ToString(); //just an obj, expecting a string
  //add to DB next
}