Backbone.js 不向服务器api发送模型值,

9ceoxa92  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(146)

下面的代码是一个很好的例子:

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);
}
b4lqfgs4

b4lqfgs41#

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

相关问题