Backbone不会将模型值发送到服务器api

时间:2016-06-15 13:28:04

标签: c# mysql asp.net-mvc backbone.js

我有以下代码作为更新用户的javascript:

updateUser: function (userResponse) {
    var userNameEdit = this.$el.find("#txtNameEdit").val();
    if (userNameEdit != "") {
        var myUser = new user();
        myUser = this.collection.models[0];
        //myUser.set({ username: 'testaaaa' });
        myUser.save({ username: userNameEdit, email: 'aaaa@aaa' }, {
            url: '/users/Edit/' + myUser.get('id'),
            wait: true,
            success: function () {
                var templateTemp = $("#personTemplate").html();
                this.$el.html(_.template(templateTemp, { name: myUser.get('name') }));
            },
            error: function () { alert('update error'); }
        });
    }
},

当我检查服务器端的用户对象时,它是默认值!:

[HttpPut]
//[ValidateAntiForgeryToken]
public ActionResult Edit(user user)
{
    if (ModelState.IsValid)
    {
        _context.Entry(user).State = EntityState.Modified;
        _context.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(user);
}

1 个答案:

答案 0 :(得分:0)

url: '/users/Edit/' + myUser.get('id'),替换为url: '/users/Edit/',