如何在服务器端和客户端验证按钮?

时间:2016-02-20 22:40:15

标签: c# asp.net-mvc

我首先尝试验证我的按钮,但我想知道我应该首先从客户端还是服务器端进行验证?如果客户端验证然后如何和服务器端验证如何?



<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>
&#13;
&#13;
&#13;

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();
    }

2 个答案:

答案 0 :(得分:0)

您应该从客户端和服务器端验证, 因为当您从客户端进行验证时,它将使您的网站更快,更具互动性,并且您无法从普通用户那里获得无效请求。 那么为什么你需要从服务器端验证? 因为任何初学者程序员都可以向您的服务器提交请求以及是否 你不会验证这个请求,你会在你的网络应用程序中遇到麻烦。

答案 1 :(得分:0)

在任何情况下,您都应始终验证服务器端。客户端验证是为您的用户提供的一项服务,使他们能够清楚地知道需要什么和不需要什么。

由于您正在使用MVC,因此您可以使用内置模型验证来处理验证所需的大量管道。您没有发布您的customerdata对象(模型),但您可以在那里添加验证属性来处理验证。

一旦启动并运行,服务器端验证就像在ModelState.IsValid操作中添加[HttpPost]一样简单。您需要在客户端上做更多的工作 - 只要您的表单无效,就不应该允许AJAX发布操作。

最重要的是,我建议你阅读MVC模型验证。您可以在此处找到详尽的解释和示例:http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-validation-to-the-model