2010-12-02 39 views
0

MVC新手在這裏。MVC +如何在控制器動作之前提醒用戶

我想控制器動作之前用戶確認(更新記錄)

我的代碼:

[HttpPost] 
    public ActionResult JobHandlerUpdate(int jobScheduleId, JobHandlerList jobHandlerList) 
    { 
     var updateJobHander = new MainJobHandler(); 
     var item = updateJobHander.GetById(jobScheduleId); 
     if (ModelState.IsValid) 
     { 
      List<string> days = jobHandlerList.JobProcessDayOfWeek.Split(',').ToList(); 
      updateJobHander.Update(item, days); 
      if(jobHandlerList.MaxInstances == 0) 
      { 

       // here I need to prompt user if maxInstances entered is Zero- 
        Job will be disabled want to processs (Y/N) if yes update 
        else do nothing or redirect to edit screen 
      } 
      return RedirectToAction("JobHandler"); 
     } 

     return View(item); 
    } 

我是否需要使用JavaScript警惕呢? 還是有一個好方法。

回答

3

你也許可以做一個onClick事件處理程序:

<input type="submit" onclick="return confirm('Are you sure you wish to submit?');" /> 

你只能做客戶端的提示,因爲控制器代碼在服務器端執行,這當然客戶端不能訪問。

1

如果您不想(或不能)使用JavaScript,請將其分爲兩步:在您驗證的一個操作中,然後重定向到執行確認的操作。 您可以將需要傳遞給TempData或Session中的確認操作的任何數據存儲。

[HttpPost] 
public ActionResult JobHandlerUpdate(int jobScheduleId, JobHandlerList jobHandlerList) 
{ 
    var updateJobHander = new MainJobHandler(); 
    var item = updateJobHander.GetById(jobScheduleId); 
    if (ModelState.IsValid) 
    { 
     List<string> days = jobHandlerList.JobProcessDayOfWeek.Split(',').ToList(); 
     updateJobHander.Update(item, days); 
     if(jobHandlerList.MaxInstances == 0) 
     { 

      // Redirect to confirmation View 
      return View("JobUpdateConfirmation"); 
     } 
     return RedirectToAction("JobHandler"); 
    } 

    return View(item); 
} 

[HttpPost] 
public ActionResult JobUpdateConfirmation() 
{ 
     // Code to update Job here 
     // Notify success, eg. view with a message. 
     return RedirectToAction("JobHandlerUpdateSuccess"); 
} 

您將需要一個形式,要求確認,並張貼回JobUpdateConfirmation視圖(JobUpdateConfirmation)。 這是一般的想法,您可以根據需要添加更多消息或步驟。

+0

CGK - 你可以把控制器操作的一些示例這確實cofimation請。 – Sreedhar 2010-12-02 00:35:03

0

我認爲這是更多的UI流程設計,而不是控制器設計。在提交給第一個控制器之前,是否可以提醒用戶有關未決更改?

我認爲JavaScript /客戶端確認將是理想的在這裏。

或者您可以以CGK建議的方式以及頁面提交後的方式執行此操作,重定向到第二個控制器,或許在其實際更新記錄之前通過視圖獲取用戶確認,或者如果他選擇不然重新導向回到上一頁。

我打算給其他答案試圖說的內容添加評論,但由於我不能100%肯定,我以爲我只是寫了我在這裏想到的。

歡呼:)

1

在ASPX:

<%= Html.ActionLink("link text", "ActionName", "ControllerName", new { actionMethodVariable = item.ID }, new { @onclick = "return confirm_dialog();" })%> 

<script type="text/javascript"> 
    function confirm_dialog() { 
     if (confirm("dialog text") == true) 
      return true; 
     else 
      return false; 
    } 
</script> 


//controller 

public ActionResult ActionName(int laugh) 
{ 
if (ModelState.IsValid) 
    { 
    //bla bla bla 
    } 

return something; 
} 
+0

除非您需要編寫自定義確認對話框,否則不需要編寫上述JavaScript函數。 – 2012-04-02 17:55:50

相關問題