public class addcustomerController : Controller { private SqlConnection con; // GET: addcustomer public ActionResult customerdetail() { return View("customerdetail"); } [HttpPost] public ActionResult customerdetail(customerdata obj) { if (ModelState.IsValid) { custmerinfo(obj); return View("customerdetail"); // return RedirectToAction("customerdetail", obj); } else { 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(); } }
public class customerdata { [Required(ErrorMessage="Please Enter your Name")] public string name { get; set; } [Required(ErrorMessage = "Please Enter your City")] public string city { get; set; } [Required(ErrorMessage = "Please Enter your Address")] public string address { get; set; } }
@{ ViewBag.Title = "customerdetail"; } <h2>Customerdetail</h2> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <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>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)