简体   繁体   English

如何使用 jquery 在 asp.net mvc 中更新纯 html 文件中的记录

[英]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>

You can use the method below to send your data to your Controller method using Ajax :您可以使用以下方法使用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);
                 }
             }
        })
    })
})

And your Controller method will look like:您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM