[英]how to update records in pure html file in asp.net mvc using jquery
<script>
$(function ()
{
var id = Request.QueryString["id"]
$.get("/Employee/getEditId", { id: id }, function (data) {
$("#txtname").val(data[0].custname);
$("#txtcountry").val(data[0].country);
});
});
$(function ()
{
$("#edit").click(function () {
$.ajax({
type: "POST",
url: "/Employee/EditEmp",
data: {
custname: $("#txtname").val(),
country: $("#txtcountry").val(),
},
success: function (data) {
clear();
alert(data);
}
})
})
})
function clear() {
$("#custname").val(''),
$("#country").val('')
}
</script>
您可以使用以下方法使用Ajax
將數據發送到您的Controller
方法:
$(function ()
{
$("#edit").click(function () {
//Get your data here
var custname=$("#txtname").val();
var country=$("#txtcountry").val();
var json = {
custname: custname,
country: country
};
$.ajax({
type: 'POST',
url: "/Employee/EditEmp",
dataType: "json",
data: {"json": JSON.stringify(json)},
success: function (data) {
if(data.status=="true")
{
clear();
alert(data.msg);
}
}
})
})
})
您的Controller
方法將如下所示:
using System.Web.Script.Serialization;
[HttpPost]
public ActionResult EditEmp(string json)
{
var serializer = new JavaScriptSerializer();
dynamic jsondata = serializer.Deserialize(json, typeof(object));
//Get your variables here from AJAX call
var custname= Convert.String(jsondata["custname"]);
var country= Convert.String(jsondata["country"]);
//Your logic to update the data
return Json(new {status="true", msg= "Successfully updated data"});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.