2016-02-20 168 views
-2

我想首先驗證我的按鈕,但我想知道應該從客戶端還是服務器端驗證?如果客戶端驗證那麼如何和服務器端驗證如何?如何驗證服務器端和客戶端的按鈕?

<script> 
 
     $(document).ready(function() { 
 
      $("#btnsave").click(function() { 
 

 
       $.ajax(
 
       { 
 
        type: "POST", 
 
        url: "addcustomer"/"customerdetail", 
 
        data: { 
 
         Name: $("#txtname").val(), 
 
         City: $("#txtcity").val(), 
 
         Address: $("#txtaddress").val() 
 
        } 
 
       }); 
 
      }); 
 
     }); 
 
    </script> 
 
</head> 
 
<body> 
 
    <div class="container"> 
 
     <form class="form-horizontal" role="form"> 
 
      <div class="form-group"> 
 
       <label class="control-label col-sm-2" for="name">Name</label> 
 
       <div class="col-sm-10"> 
 
        <input type="text" class="form-control" id="txtname" placeholder="Enter your Name"> 
 
       </div> 
 
      </div> 
 
      <div class="form-group"> 
 
       <label class="control-label col-sm-2" for="city">City</label> 
 
       <div class="col-sm-10"> 
 
        <input type="text" class="form-control" id="txtcity" placeholder="Enter your City"> 
 
       </div> 
 
      </div> 
 
      <div class="form-group"> 
 
       <label class="control-label col-sm-2" for="address">Address</label> 
 
       <div class="col-sm-10"> 
 
        <input type="text" class="form-control" id="txtaddress" placeholder="Enter your Address"> 
 
       </div> 
 
      </div> 
 
      <div class="form-group"> 
 
       <div class="col-sm-offset-2 col-sm-10"> 
 
        <button type="button" class="btn-default" id="btnsave">Save</button> 
 
       </div> 
 
      </div> 
 
     </form> 
 
    </div> 
 
</body> 
 
</html>

private SqlConnection con; 
    // GET: addcustomer 
    public ActionResult customerdetail() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult customerdetail(customerdata obj) 
    { 
     custmerinfo(obj); 

     return View(); 
    } 


    private void connection() 
    { 
     string constr = ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString; 
     con = new SqlConnection(constr); 
    } 

    public void custmerinfo(customerdata obj) 
    { 
     connection(); 
     SqlCommand com = new SqlCommand("addcustomer", con); 
     com.CommandType = CommandType.StoredProcedure; 
     com.Parameters.AddWithValue("@name", obj.name); 
     com.Parameters.AddWithValue("@city", obj.city); 
     com.Parameters.AddWithValue("@address", obj.address); 
     con.Open(); 
     com.ExecuteNonQuery(); 
     con.Close(); 
    } 
+0

絕不信任用戶輸入。始終進行服務器端驗證。客戶端驗證更具響應性並減少無效請求的數量。 – Jasen

+0

你可以從這裏開始http://www.asp.net/mvc/overview/getting-started/introduction/adding-validation。而且,由於您使用AJAX發佈信息,因此您需要查看jQuery驗證。 – Jasen

+0

驗證什麼?你甚至沒有把你的視圖綁定到模型上。我建議你去MVC網站,通過教程學習MVC的基礎知識。通過使用驗證屬性修飾您的屬性,綁定到您的模型併發布它,所有這些都是通過盒子(客戶端和服務器端驗證)處理的。 –

回答

0

應驗證,當你從客戶端驗證它會使你的網站更快,更互動的形式在客戶端和服務器端, 原因,你贏不會從普通用戶那裏得到無效的請求。 那麼爲什麼你需要從服務器端驗證? ,因爲任何初學者程序員都可以向服務器提交請求,並且如果您不驗證此請求,則會在Web應用程序中遇到問題。

0

無論如何,您應該始終驗證serverside。客戶端驗證是向用戶提供的一項服務,以便向他們說明需要什麼以及不需要什麼。

由於您使用的是MVC,因此您可以使用內置模型驗證來處理驗證所需的大量管道。您沒有發佈您的customerdata對象(模型),但是您可以將驗證屬性添加到那裏的屬性中以處理驗證。

一旦啓動並運行,服務器端驗證與在[HttpPost]操作中添加ModelState.IsValid一樣簡單。您需要在客戶端做更多的工作 - 只要您的表單無效,就不應允許AJAX發佈後操作啓動。

最重要的是,我建議你閱讀MVC模型驗證。您可以在這裏找到大量的解釋和示例:http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

+0

非常感謝您的回答 –

相關問題