Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么Ajax编辑代码不能正常工作?你能帮助我吗?_Javascript_C#_Jquery_Ajax_Asp.net Mvc - Fatal编程技术网

Javascript 为什么Ajax编辑代码不能正常工作?你能帮助我吗?

Javascript 为什么Ajax编辑代码不能正常工作?你能帮助我吗?,javascript,c#,jquery,ajax,asp.net-mvc,Javascript,C#,Jquery,Ajax,Asp.net Mvc,我在做简单的注册,我有两个表格一个是注册,另一个是城市,当城市是新添加的,它会得到完美的更新,但当我在注册表格中使用城市,例如pune。pune不会被编辑或更新,代码是用ajax编写的 function UpdateCity(Ids) { debugger; var Id = { Id: Ids } $('#UpdateModel').modal('show'); $.ajax({ type: 'GET', url: "/City

我在做简单的注册,我有两个表格一个是注册,另一个是城市,当城市是新添加的,它会得到完美的更新,但当我在注册表格中使用城市,例如pune。pune不会被编辑或更新,代码是用ajax编写的

function UpdateCity(Ids) {
    debugger;
    var Id = { Id: Ids }
    $('#UpdateModel').modal('show');

    $.ajax({
        type: 'GET',
        url: "/City/GetCityDetail",
        data: Id,
        dataType: "json",
        success: function (city) {
            $('#EditCityName').val(city.CityName);
            $('#EditCityId').val(city.CityId);
        }
    })



    $('#UpdateCityButton').click(function () {
        var model = {
            CityName: $('#EditCityName').val(),
            CityId: $('#EditCityId').val()
        }
        debugger;
        $.ajax({
            type: 'POST',
            url: "/City/UpdateCity",
            data: model,
            dataType: "text",
            success: function (city) {
                $('#UpdateModel').modal('hide');
                bootbox.alert("City updated");
                window.setTimeout(function () { location.reload() }, 3000)
            }
        })
    })
}
控制器

public bool UpdateCity(City model, long CurrentUserId)
{
    try
    {
        var city = db.Cities.Where(x => x.CityId == model.CityId && x.IsActive == true).FirstOrDefault();
        if (city == null) return false;
        city.CityName = model.CityName;
        city.UpdateBy = CurrentUserId;
        city.UpdateOn = DateTime.UtcNow;
        db.SaveChanges();
        return true;
    }
    catch (Exception Ex)
    {
        return false;
    }
}

这里有一些暗中操作,但是,请尝试将代码更改为以下内容(带注释)

控制器:

// !! This is a POST transaction from ajax
[HttpPost]
// !! This should return something to ajax call
public JsonResult UpdateCity(City model, long CurrentUserId)
{
    try
    {
        var city = db.Cities.Where(x => x.CityId == model.CityId && x.IsActive == true).FirstOrDefault();
        if (city == null) return false;
        city.CityName = model.CityName;
        city.UpdateBy = CurrentUserId;
        city.UpdateOn = DateTime.UtcNow;
        db.SaveChanges();
        // !! Change return type to Json
        return Json(true);
    }
    catch (Exception Ex)
    {
        // !! Change return type to Json
        return Json(false);
    }
}
脚本:

function UpdateCity(Ids) {
    //debugger;
    var Id = { Id: Ids };
    $('#UpdateModel').modal('show');
    $.ajax({
        type: 'GET',
        url: "/City/GetCityDetail",
        data: Id,
        dataType: "json",
        success: function (city) {
            $('#EditCityName').val(city.CityName);
            $('#EditCityId').val(city.CityId);
        },
        error: function () {
            // !! Change this to something more suitable   
            alert("Error: /City/GetCityDetail");
        }
    });

    $('#UpdateCityButton').click(function () {
        var model = {
            CityName: $('#EditCityName').val(),
            CityId: $('#EditCityId').val()
        };
        //debugger;
        $.ajax({
            type: 'POST',
            url: "/City/UpdateCity",
            data: model,
            // !! Change return type to Json (return type from Server)
            dataType: "json",
            success: function (city) {
                // !! Check result from server
                if (city) {
                    $('#UpdateModel').modal('hide');
                    bootbox.alert("City updated");
                    // !! Why reload location?
                    // window.setTimeout(function () { location.reload(); }, 3000);
                } else{
                    // !! Change this to something more suitable   
                    alert("Server Error: /City/UpdateCity");
                }
            },
            error: function () {
                // !! Change this to something more suitable   
                alert("Error: /City/UpdateCity");
            }
        });
    });
}

这将为您提供更多关于发生了什么的线索。

您遇到了什么错误?函数UpdateCity(Ids)是否正确关闭?当新添加city时,它会得到完美的添加更新,但当我在注册表中使用city时,例如pune。pune将不会被编辑或更新。它是否正在访问public bool UpdateCity(城市模型,长CurrentUserId)?请提供更多信息。例如:您是否尝试在服务器端调试UpdateCity操作?ajax调用是否会影响服务器上的操作?