【问题标题】:how to update records in pure html file in asp.net mvc using jquery如何使用jquery在asp.net mvc中更新纯html文件中的记录
【发布时间】:2022-02-07 21:36:42
【问题描述】:
<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>

【问题讨论】:

    标签: c# jquery ajax asp.net-mvc


    【解决方案1】:

    您可以使用以下方法将您的数据发送到您的Controller 方法,使用Ajax

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多