2011-12-18 29 views
1

我做的這個小教程到控制器:http://www.asp.net/mvc/videos/mvc-2/how-do-i/creating-a-tasklist-application-with-aspnet-mvc傳遞一個字符串中使用ASP 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 
} 

回答

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 
} 
相關問題