我想知道在asp.net mvc 3 RC中提交表單時如何執行多個表單操作。如何最好地實現Save |保存並關閉|在ASP.NET MVC 3中取消表單操作RC
如果我正在編輯用戶,例如我想要一個帶有以下按鈕的操作欄;
「Save」| 「保存並關閉」| 「取消」
保存 - 提交表單並保存,返回到編輯屏幕。可以輕鬆實現爲標準輸入/提交按鈕。沒什麼特別的。
這個控制器代碼可能看起來像
public ActionResult Edit(UserViewModel model)
{
...
return RedirectToAction("Edit", model.Id");
}
取消 - 剛剛返回到前一個畫面。我正在考慮爲此使用錨標籤。
<a href="@Request.UrlReferrer" class="button">Cancel</a>
但我難倒就如何落實「保存並關閉」當你需要提交相同的表格數據。我想知道有一個可爲空的關閉參數也許?
public ActionResult Edit(UserViewModel model, bool? close)
{
...
return close.GetValueOrDefault(false) ? RedirectToAction("Index", model.Id" : RedirectToAction("Edit", model.Id");
}
但是,在這種情況下,我如何提交這個額外的參數?
如果可能,我希望有一個單一的表單操作來處理提交,如上面的模型。
我也很感興趣,如果有人提出了一個很好的用戶交互模型圍繞這個想法。
解決方案
最後我用下面奧馬爾的建議,但不是在傳遞一個字符串,我參加了一個枚舉所以我沒有做字符串檢查在我所有的控制器。
public ActionResult Edit(UserViewModel model, FormAction actionType)
{
// pre-check
if (actionType == FormAction.Cancel)
// just return user to previous view and don't save.
// Save code
if (actionType == FormAction.Save)
return ...
else if (actionType == FormAction.SaveAndClose)
....
}
因爲我想在<input>
按鈕友好「保存並關閉」文字,但想用一個枚舉我實現了FormAction自定義模型綁定器是做了解析。
我沒有使用<button>
標籤,因爲主題已爲<input>
標籤。
http://stackoverflow.com/questions/2423041/using-two- submit-buttons-inside-single-form/2426152#2426152 – takepara 2010-11-24 15:38:34
謝謝奧馬爾。那正是我想要的。乾杯! – 2010-11-24 23:15:21