Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未捕获类型错误_C#_Jquery_Asp.net Mvc_Signalr - Fatal编程技术网

C# 未捕获类型错误

C# 未捕获类型错误,c#,jquery,asp.net-mvc,signalr,C#,Jquery,Asp.net Mvc,Signalr,我正在编写我的第一个信号器代码,我遇到了一个未捕获类型的错误。我的代码最初可以工作,因为我的集线器控制器将序列化数组返回给客户端。当我尝试使用$.each()方法遍历数组时,就会出现问题。我收到的确切错误是: 未捕获的TypeError:无法使用“in”运算符在中搜索“95” [{“OptionsId”:3,“会员级别”:“黄金”,“会员级别ID”:2,“天”:0,“月”:1,“费用”:20.00}] 我不知道这意味着什么,所以我不确定这是与信号器相关的、jQuery还是其他什么。这是我的密码

我正在编写我的第一个信号器代码,我遇到了一个未捕获类型的错误。我的代码最初可以工作,因为我的集线器控制器将序列化数组返回给客户端。当我尝试使用$.each()方法遍历数组时,就会出现问题。我收到的确切错误是:

未捕获的TypeError:无法使用“in”运算符在中搜索“95” [{“OptionsId”:3,“会员级别”:“黄金”,“会员级别ID”:2,“天”:0,“月”:1,“费用”:20.00}]

我不知道这意味着什么,所以我不确定这是与信号器相关的、jQuery还是其他什么。这是我的密码

jQuery:

var getOptions = function () {
// reference the auto generated proxy for the hub
var vR = $.connection.vendorRegistration;

// create the function that the hub can call back to update option
vR.client.updateMembershipOptions = function (returnOptions) {
    // update the selectList
    $("select[name=SelectedMembershipOption]").empty();

    // this is where I receive the error
    $.each(returnOptions, function (index, memOption) {
        $("select[name=SelectedMembershipOption]").append(
            $("<option/>").attr("value", memOption.OptionsId)
              .text(memOption.MembershipLevel)
            );
    });
};

// Start the connection
$.connection.hub.start().done(function () {
    $("#VendorType").change(function () {
        // call the ReturnMembershipOptions method on the hub
        vR.server.returnMembershipOptions($("#VendorType")
            .val());
    });
});
};

之所以会出现这样的错误,是因为您试图遍历字符串。它试图遍历字符串的原因是因为这一行:

// serialize the options
var returnOptions = JsonConvert.SerializeObject(options);
signer将为您序列化对象,因此,由于您在hub方法中序列化,因此通过连接的数据是双重序列化的,然后表示一个普通字符串,因此当它到达客户端时,它将反序列化为一个字符串

对字符串执行.each将抛出,因为它要求调用参数为数组或对象

要解决问题,只需删除序列化并使用选项调用函数,即:

public class VendorRegistration : Hub
{
    public void ReturnMembershipOptions(int vendorTypeId)
    {
        // get the options
        var options = AcoadRepo.Vendors.ApiLogic.MembershipOptions
            .ReturnOptions(vendorTypeId);

        // call the updateMembershipOptions method to update the client
        Clients.All.updateMembershipOptions(options);
    }
}
public class VendorRegistration : Hub
{
    public void ReturnMembershipOptions(int vendorTypeId)
    {
        // get the options
        var options = AcoadRepo.Vendors.ApiLogic.MembershipOptions
            .ReturnOptions(vendorTypeId);

        // call the updateMembershipOptions method to update the client
        Clients.All.updateMembershipOptions(options);
    }
}