0

我有以下使用内部类的 KnockoutJS 映射:

public class Retailer
{
    public int RetailerId { get; set; }
    public string DemoNumber { get; set; }
    public string OutletName { get; set; }
    public string OwnerName { get; set; }
    public Address Address { get; set; }

    public Logging Logging { get; set; }
}

public class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string Mobile { get; set; }
    public string LandLine { get; set; }
    public string Email { get; set; }
}

上面显示了我的模型,我正在使用 KnockoutJS 构建我的网站。

我可以使用ko.mapping插件绑定控件。但是,当我尝试插入 Retailer 的值时,内部类Address正在返回null所有属性的值。

当我在客户端测试东西时,它会显示整个模型值。

IdeaSales.NewRetailer = function () {
    $.ajax({
        url: '/Retailer/Retailer',
        dataType: 'json',
        cache: false,
        type: "post",
        success: function (data) {
            viewModel = ko.mapping.fromJS(data);
            ko.applyBindings(viewModel);
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
}

IdeaSales.InsertRetailer = function () {
    var newModel = ko.toJS(viewModel);
    $.ajax({
        url: '/Retailer/InsertRetailer',
        data:viewModel,
        cache: false,
        type: "post",
        success: function (data) {

        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
}    

和:

public int InsertRetailer(Retailer retailer)
{
    return new RetailerAppService().InsertRetailer(retailer);
}

问题是我正在获取零售商信息,但对于 Address 类,我正在获取Null每个值。

4

1 回答 1

1

当您将 JSON 发送到服务器时,您需要指定 contentType。

这个版本的 InsertRetailer 应该可以工作:

IdeaSales.InsertRetailer = function () {
    var newModel = ko.toJS(viewModel);
    $.ajax({
        url: '/Retailer/InsertRetailer',
        contentType: "application/json",
        data: newModel,
        cache: false,
        type: "post",
        success: function (data) {

        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });
}   
于 2013-05-26T20:53:48.213 回答