使用Marionette js和Web API 2进行表单提交的最佳实践

时间:2016-08-05 18:33:35

标签: javascript asp.net-web-api2 marionette

Web API 2具有参数绑定。木偶有模特。如何以最有效的方式连接两者(例如,成功提交表单)?

这是我失败的尝试:

    var attributes = $('#myForm').serialize();

    this.model.save(attributes, {
        success: function (model, response) {
            console.log('success! ' + response);
        },
        error: function (model, response) {
            console.log('error! ' + response);
        }
    });

没有错误,但也没有传递给我的Web API控制器的值。我在保存之前看到属性中的值。但是服务器上没有发生Web API参数绑定。

然而,标准的ajax提交确实有效:

    $.ajax({
        url: 'http://myresturl',
        type: 'POST',
        dataType: 'json',
        data: $('#myForm').serialize(),
        success: function (data, textStatus, xhr) {
            console.log(data);
        },
        error: function (xhr, textStatus, errorThrown) {
            console.log(textStatus);
        }
    });

我认为模特只是引擎盖下的ajax?我不应该使用该模型提交(保存)?什么是正确的方法?

0 个答案:

没有答案