我想首先驗證我的按鈕,但我想知道應該從客戶端還是服務器端驗證?如果客戶端驗證那麼如何和服務器端驗證如何?如何驗證服務器端和客戶端的按鈕?
<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();
}
絕不信任用戶輸入。始終進行服務器端驗證。客戶端驗證更具響應性並減少無效請求的數量。 – Jasen
你可以從這裏開始http://www.asp.net/mvc/overview/getting-started/introduction/adding-validation。而且,由於您使用AJAX發佈信息,因此您需要查看jQuery驗證。 – Jasen
驗證什麼?你甚至沒有把你的視圖綁定到模型上。我建議你去MVC網站,通過教程學習MVC的基礎知識。通過使用驗證屬性修飾您的屬性,綁定到您的模型併發布它,所有這些都是通過盒子(客戶端和服務器端驗證)處理的。 –