2012-04-21 19 views
0

我有一個MVC 3網站,當用戶試圖添加同一日期的記錄時,我需要設置一個警告與確認。用戶點擊視圖上的按鈕:如何觸發控制器的彈出確認

<input type="button" onclick="document.location.href = '@Url.Action("StartClock", "Time")'" value="Start Clock" /> 

這是來自控制器的代碼。按鈕tat觸發此操作位於StartWork視圖中。

public ActionResult StartWork() 
{ 
    return View(); 
} 

public ActionResult StartClock() 
{ 
    if (helper.IsDuplicate(1, DateTime.Now)) 
    { 
     //here I want to trigger a confirmation popup or some warning 
    } 
    helper.Start(1, DateTime.Now); 
    return RedirectToAction("Index"); 
} 

當它是重複的我想要觸發某種類型的彈出式警告和選項繼續或取消。

我試過渲染PartialView,但它只是呈現新的看法。

任何幫助非常感謝!

回答

2

嘗試使用標誌ViewData設置爲指示是否確認框應該在你的視圖中顯示:

if (helper.IsDuplicate(1, DateTime.Now)) 
{ 
    ViewData["RequireConfirmation"] = true; 
} 
在你看來

然後:

@if ((bool)ViewData["RequireConfirmation"]) { 
    var answer = confirm ("Are you sure you want to add this record?") 
    if (answer) { 
     //Confirmed 
    } 
}