I´m having difficulties matching Javascript Ajax to ASP.NET Controller type. All parameters comes fine, except for the MsgData that comes null when received by the Controller.
My Controller classes and code:
public class AjaxMessage
{
public enum MsgStatusType : int { OK, NOK }
public string MsgType { get; set; }
public List<AjaxMessageItem> MsgData { get; set; }
public MsgStatusType MsgStatus { get; set; }
public string MsgStatusMsg { get; set; }
}
And
public class AjaxMessageItem
{
public string Name { get; set; }
public string Value { get; set; }
public AjaxMessageItem()
{
Name = String.Empty;
Value = String.Empty;
}
}
Finally:
public ActionResult Get(AjaxMessage ajaxMessage)
{
... do some stuff...
}
My Javascript call:
var url = '@Url.Action("Get", "AjaxServer")';
$.ajax({
url: url,
cache: false,
type: "POST",
data: {
'MsgType': 'MsgType1',
'MsgData': { 'Name': 'customerId', 'Value': 'current' },
'MsgStatus': 'OK',
'MsgStatusMessage' : ''
},
success: function (data) {
if (data.msgStatus != 'OK') {
var errorMsg = 'Error reading data.' + data.msgStatusMessage;
alert(errorMsg);
return;
}
... do some stuff...
}
},
error: function (data) {
alert('Error retrieving Ajax data.');
}
});
At controller, I´m getting MsgType, MsgStatus and MsgStatusMsg fine whan looking for ajaxMessage variable, but MsgData is always null and shall be receiving the customerId data.
I need some help to solve that. Please help me to find out what´s missing... Thanks.
MsgDataas an array. Don't know if it is just a typo perhaps but it should probably be:'MsgData': [{ 'Name': 'customerId', 'Value': 'current' }],.