我正在做这个小教程: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
}
答案 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
}