theStig theStig - 1 month ago 14
jQuery Question

SignalR error converting value

First time working with SignalR and I'm running into some issues. What I'm trying to do is asynchronously update my jTable once a user adds a new user to the system. The problem is, every time I try to submit, I run into the error below. I'm not sure what the problem is, as all the data needed to populate my CreateUserModel is passed through. Any thoughts as to what I'm doing wrong?

The Error

{"I":"0","E":"Error converting value \"UserId=19&FirstName=123&LastName=123&UserName=123&Role=Admin&Password=123123&ConfirmPassword=123123&EmailAddress=fakeEmail%40gmail.com&PhoneNumber=123\" to type 'MyProject.Models.CreateUserModel'.


The Model I'm trying to convert to

public class CreateUserModel {
public int UserId { get; set; }

public string UserName { get; set; }

public string Password { get; set; }

public string ConfirmPassword { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public string EmailAddress { get; set; }

public string PhoneNumber { get; set; }

public string Role { get; set; }
public IEnumerable<SelectListItem> RolesItem {
get { return new SelectList(UserUtils.getAllRoles()); }
}
}


Hub

public class JTableHub : Hub {
public void UpdateUserTable(CreateUserModel userModel) {
Clients.All.updateTable(userModel);
}
}


signalR code

<script type="text/javascript">
$(document).ready(function () {
var chat = $.connection.jTableHub;
chat.client.updateTable = function (userModel) {
$('#usersTable').jtable('addRecord', {
record: userModel,
clientOnly: true
});
};

$.connection.hub.start().done(function () {
$('#btnAdd').click(function () {
var data = $('#addUserForm').serialize();
chat.server.updateUserTable(data);
});
});


});
</script>

Answer

.serialize can't be used as it sends form url encoded data. SignalR only supports JSON.