如何将使用GET返回的字符串添加到骨干模型

本文关键字:添加 字符串 模型 返回 GET | 更新日期: 2023-09-27 18:14:42

我有这个ApiController,它只从表中返回一列。

public IEnumerable<string> GetEmailConfig()
{
    return dbContext.EmailConfig.Select(m => m.smtpHost).AsEnumerable();
}

我只需要返回smtpHost,但不返回数据库表中的其他字段。它返回为(smtp.host)0:"smtp.host"

我如何将此添加到我的骨干模型正确?目前,它显示在如下属性中:

attributes: Object
0: "["
1: """
2: "s"
3: "m"
4: "t"
5: "p"
6: "."
7: "a"
8: "b"
9: "c"
10: "."
11: "d"
12: "e"
13: "f"
14: """
15: "]"

我认为我必须序列化字符串,但不确定如何做到这一点,或者如果这就是问题所在。有什么建议吗?

编辑:我的模型

App.Models.EmailConfigModel = Backbone.Model.extend({
    parse: function(response, option)
    {
        var smtpHost = "";
        console.log('parsing smtp host');
        $.each(response, function (index, val) {
            smtpHost = smtpHost + val;
        });
        return { "smtpHost": smtpHost };
    }
});

我也尝试了return{"attributeName": smtpHost };和"attributename"。没有工作。

这是我如何从我的索引页

App.emailconfig.fetch().then(function () {
    new App.Views.EmailConfig({ collection: App.emailconfig });
});

这是我的收藏:

App.Collections.EmailConfig = Backbone.Collection.extend({
    model: App.Models.EmailConfigModel,
    url: 'api/EmailConfig',
});

如何将使用GET返回的字符串添加到骨干模型

parse在模型的数据被服务器返回、获取和保存时被调用。将原始响应对象传递给函数,并且应该返回要在模型上设置的属性哈希值

所以在你的模型中写一个解析函数,如

parse : function(response,option){
  var mystr = "";
    $.each($.parseJSON(response), function(index, val) {
             mystr = mystr + val;
        });
   return {"attributenmae" : mystr};
}

写一个返回对象的解析方法{attributeName:stringReturnedFromService}