2011-10-16 18 views
0

嗨,我正在使用MVC3和我在C#環境。我想在滿足else語句時彈出一個顯示。 的代碼如下所示:如何通過在C#中的else語句觸發彈出窗口

if (validate.Count < 1) { 
    db.TutorStudents.Add(tutorStudent); 
    db.SaveChanges(); 
    return RedirectToAction("Index"); 
} else { 
    // need some form of popup message here that does not redirect the user       
} 

我一直在尋找,我發現JQuery的的解決方案,但他們在點擊結果工作權......相反,我想在彈出的工作else語句時到達了。任何解決方案都是受歡

預先感謝您

+0

你的陳述究竟在哪裏? –

回答

2

您無法直接在服務器端顯示客戶端彈出窗口。

但是你可以把一些標記值的ViewDataViewBag,並在您查看此值傳遞給腳本:

在行動

 if (validate.Count < 1) 
     { 
      db.TutorStudents.Add(tutorStudent); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     else 
     { 
      ViewBag.ShowMessage = true; 
      //need some form of popup message here that does not redirect the user        
     } 

視圖

@if (ViewBag.ShowMessage) 
{ 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $(".message-box").show(); 
     }); 
    </script> 
    <div class="message-box">Some Message here</div> 
} 
+0

這正是我一直在尋找的......非常感謝你 – futureMVCwhizz

+0

儘管在視圖中的聲明中我認爲你在@if語句中忘記== true ......但它很好用 – futureMVCwhizz

2

嘗試:

if (validate.Count < 1){ 
    db.TutorStudents.Add(tutorStudent); 
    db.SaveChanges(); 
    return RedirectToAction("Index"); 
} else { 
    ViewBag.PopUp = true; 
    return View(); // or any logic to show the last-view to user again       
} 

,並考慮:

@if(ViewBag.PopUp == true){ 
    <script type="text/javascript"> 
     alert("Your message here..."); 
    </script> 
} 

UPDATE:無法通過C#在Web的顯示彈出用戶應用。由於C#在服務器上運行,您必須創建客戶端代碼才能在最終用戶瀏覽器上運行。所以,您必須通過C#創建一個邏輯來控制應用程序並生成HTML(和/或JavaScript,jQuery,CSS)以實現您的目的。

相關問題