I have a form I am trying to send in a GetJSON call. When I get to the Controller the model that si tied to the view is a null vlaue. I have had issues before dealign with returning data when I woudl get an empy object but never a null value. Below is the code I am using to send the form
var cqvdata = $("form").serialize();
$.getJSON('@Url.Action("GetEmailByAdvanced", "CustomerEmails")', { cqv: cqvdata }, function (contacts) {
var emails = "";
$.each(contacts, function (index, contact) {
$('#BCCText').tagit('createTag', contact.Email)
});
return false;
});
Below is what I have on the controller side
public JsonResult GetEmailByAdvanced(MassEmailViewModel cqv)
{
}
Here is what I get for results if I turn my argument into a string
"EmailFromAddressID=1&ToAddresses=&CCAddresses=bclairmont%40harr.com&BCCAddresses=adunn%40harr.com&Subject=&Body="
Below is the MassEmailViewModelClass and all sub classes
public class MassEmailViewModel
{
public MassEmailViewModel()
{
ComplexQuery = new CustomerQueryViewModel();
}
public int EmailFromAddressID { get; set; }
public CustomerQueryViewModel ComplexQuery { get; set; }
public string ToAddresses { get; set; }
public string CCAddresses { get; set; }
public string BCCAddresses { get; set; }
public string Subject { get; set; }
[AllowHtml]
public string Body { get; set; }
}
public class CustomerQueryViewModel
{
public CustomerQueryViewModel()
{
Products = new List<CustomerProductQueryProduct>();
Details = new List<CustomerQueryDetail>();
}
public Boolean IncludeOnAll { get; set; }
public Boolean ExcludeOnAll { get; set; }
public List<CustomerProductQueryProduct> Products { get; set; }
public List<CustomerQueryDetail> Details { get; set; }
}
public class CustomerProductQueryProduct
{
public CustomerProductQueryProduct()
{
ProductDetails = new List<CustomerProductQueryProductDetail>();
ProductVersions = new List<ProductVersion>();
}
public ProductType ProductType { get; set; }
public Boolean Exclude { get; set; }
public Boolean Include { get; set; }
public int VersiondID { get; set; }
public List<CustomerProductQueryProductDetail> ProductDetails { get; set; }
public List<ProductVersion> ProductVersions { get; set; }
}
public class CustomerProductQueryProductDetail
{
public ProductTypeDetail ProductDetail { get; set; }
public Boolean Exclude { get; set; }
public Boolean Include { get; set; }
public string Value { get; set; }
public string Value2 { get; set; }
}
public class CustomerQueryDetail
{
public string Description { get; set; }
public string Type { get; set; }
public Boolean Exclude { get; set; }
public Boolean Include { get; set; }
public string Value { get; set; }
public string Value2 { get; set; }
}
The only thing not being returned is my ComplexQuery in the serialize because I am using a JQuery dialog so it takes those elements out of the form. I woudl think I woudl get a MassEmaikViewModel with all the vlaues but ComplexQuery and have a null for that but I just get a null as iff the argument never even got initialized.
Any ideas on what could be causing this?
One other thing and I don’t know if this will help give anyone any insight or not but I can post from the form and have the MassEmailViewModel as the argument in the post and it works fine filling out all the values except for ComplexQuery
I figured it out after a ton of trial and error. It seems like GetJSON can’t handle passing the data. What I did to correctly get information was to change to an AJAX get call. I will post the code below
I used the exact data I had in the GetJSON. In fact I commented out the GetJSON and just put this in below it and I got my model filled in on the controller side.