2014-09-11 39 views
1

我正在使用WebMatrix和jQuery Mobile來開發涉及一些數據庫操作的簡單移動網頁。其中一個要求是顯示類似警報的對話框,以通知用戶操作的方式。我已經做了一些研究,但是當談到某些數據庫操作時,我找不到有效的解決方案。以下是我能做的最好的,但似乎有一些問題。使用jQuery Mobile在數據庫插入後顯示對話框或彈出框

  1. 數據插入成功,但它始終在catch塊中進行重定向,忽略try塊中的重定向。

  2. 這絕對不是一個好習慣。有人能給我一個方向來更有效地實現這一點嗎?

在此先感謝。

Default.cshtml

@{ 
    Layout = "~/_Layout.cshtml"; 
    Page.Name = "Test Adding Records"; 
    if (IsPost) 
    { 
     string strFirst, strSecond; 
     strFirst = Request.Form["first"]; 
     strSecond = Request.Form["second"]; 

     var database = Database.Open("TestDB"); 
     string strSql = "INSERT INTO TestTable " + 
      "(firstCol, secondCol) " + 
      "VALUES(@0, @1);"; 
     try{ 
      database.Execute(strSql, strFirst, strSecond); 
      Response.Redirect("~/Msg.cshtml?msg=Successfully"); 
     } 
     catch(Exception ex){ 
      Response.Redirect("~/Msg.cshtml?msg=failed"); 
     } 
    } 
} 

<div data-role="page"> 
    <div data-role="header"> 
     <h1>Add a Record</h1> 
    </div> 
    <div data-role="content"> 
     <form method="post" action=""> 
      <div data-role="fieldcontain"> 
       <label for="first">First Column:</label> 
       <input type="text" name="first" id="first" value="@Request.Form["first"]" /> 
      </div> 
      <div data-role="fieldcontain"> 
       <label for="second">Second Column:</label> 
       <input type="text" name="second" id="second" value="@Request.Form["second"]" /> 
      </div> 
      <div data-role="fieldcontain"> 
       <input type="submit" value="Add Record" /> 
      </div> 
     </form> 
    </div> 
</div> 

Msg.cshtml

@{ 
    string message = Request.QueryString["msg"]; 
    string previousUrl = Request.UrlReferrer.ToString(); 
} 

<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> 
    </head> 
    <body> 
     <script type="text/javascript"> 
      alert('@message'); 
      window.location.replace('@previousUrl'); 
     </script> 
    </body> 
</html> 
+0

我錯過了jQuery?我沒有看到任何。 – 1252748 2014-09-11 01:06:09

+0

確實。這只是顯示對話的簡單測試。任何幫助表示讚賞。 – 2014-09-11 06:25:49

回答

0

嘗試用這種小的修改

try{ 
     database.Execute(strSql, strFirst, strSecond); 
    } 
    catch(Exception ex){ 
     Response.Redirect("~/Msg.cshtml?msg=failed"); 
    } 
    Response.Redirect("~/Msg.cshtml?msg=Successfully");