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

时间:2018-03-27 06:14:04

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

我正在进行简单的登记我有两种形式,一种是登记,另一种是城市,在新添加市的时候,完全添加更新,但是在登记形式例如浦那使用市的时候。 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;
    }
}

1 个答案:

答案 0 :(得分:0)

这里有一些黑暗,但是,尝试将您的代码更改为以下内容(带注释)。

控制器:

// !! 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");
            }
        });
    });
}

这应该会为你提供更多关于发生了什么的线索。