使用ajax mvc

时间:2016-02-04 13:49:16

标签: jquery ajax asp.net-mvc

在对他的建筑进行任何改变之前,我有对象

var beforeChangeUser = {
     "Id": row.userId,
     "Name": row.name,
     "Surname": row.surname,
     "Rate": row.rate,
     "AccName": row.accName,
     "FixCosts": row.fixCosts,
    }

有用户可能已经改变了一些propoerties

 var changedUser = {
       "Id": row.userId,
       "Name": $('#nameEdit').val(),
       "Surname": $('#surnameEdit').val(),
       "Rate": $('#rateEdit').val(),
       "AccName": $('#accountEdit').val(),
       "FixCosts": $('#fixCostsEdit').val(),
      }

我正在比较这两个对象以检查是否有任何更改

function getDiffrences(beforeChangeUser, changedUser) {
    var user = changedUser;
    var changed = false;
    if (beforeChangeUser.Name !== changedUser.Name) {
        user.Name = changedUser.Name;
        changed = true;
    }
    if (beforeChangeUser.Surname !== changedUser.Surname) {
        user.Surname = changedUser.Surname;
        changed = true;
    }
    if (beforeChangeUser.Rate !== changedUser.Rate) {
        user.Rate = changedUser.Rate;
        changed = true;
    }
    if (beforeChangeUser.AccName !== changedUser.AccName) {
        user.AccName = changedUser.AccName;
        changed = true;
    }
    if (beforeChangeUser.FixCosts !== changedUser.FixCosts) {
        user.FixCosts = changedUser.FixCosts;
        changed = true;
    }
    if (changed) {
        return user;
    } else {
        return false;
    }
}

最后有一个我正在尝试将数据发送到

的ajax
var userToChange = getDiffrences(beforeChangeUser, changedUser);
                                    if (userToChange == false) {
                                        errorMsg.text("You did not make any changes");
                                    } else {
                                        $.ajax({
                                            type: "POST",
                                            url: '/ManageUsersController/EditUser',
                                            data: userToChange,
                                            success: function (succ) {

                                            },
                                            error: function (data) {
                                            }
                                        });
                                    }

我将它发送到控制器,如下所示

  [HttpPost]
    public JsonResult EditUser(UserEditViewModel userToChange)
    {
        return Json(_userService.EditUser(new UserEditDTO
        {
            AccName = userToChange.AccName,
            FixCosts = userToChange.FixCosts,
            Id = Int32.Parse(userToChange.Id),
            Name = userToChange.Name,
            Rate = userToChange.Rate,
            Surname = userToChange.Surname,
        }));
    }

我收到错误404找不到/ ManageUsersController / EditUser

我做错了什么?

0 个答案:

没有答案