2011-01-11 114 views
0

可能是我的問題會有點混亂,我正在用asp mvc 2(我是一個初學者)構建一個簡單的預訂系統,我用它的控制器生成了CRUD視圖。當我去〜/預訂/創建出現自動生成的創建表單。但它似乎與textfields,以及我正在努力改變它們的下拉列表。問題在於,如何在所有下拉列表和文本框中使用所選值保存表格?如何在asp mvc 2中創建一個「自定義創建表單」?

謝謝!對不起,如果這是一個奇怪的問題,這是我第一次在堆棧溢出。

回答

0

您將需要使用[HttpPost]屬性裝飾的另一個操作方法,它將接受您作爲窗體呈現的相同模型對象。

因此,假設下面是你用來創建表格的操作方法:

public ActionResult Create(){ 
    var model = new Model(); 
    return View(model); 
} 

您需要創建類似下面的另一種方法:

[HttpPost] 
public ActionResult Create(Model model){ 
    if (ModelState.IsValid){ 
     //write the code to save the object here 
     return RedirectToAction("Index); //This should be the where the user would go if the "Create" operation was successful 
    } 
    return View(model); //Else return the user to the same view and show any errors. 
}