MVC3 & JSON.stringify () ModelBinding returns a null model

I'm trying to get model binding with MVC3 and JSON, but I'm out of luck ... No matter what I do, I seem to get a null model on the server.

Method Signature:

 public ActionResult FilterReports(DealSummaryComparisonViewModel model) 

Javascript UPDATED :

 <script type="text/javascript" language="javascript"> $(document).ready(function () { $('#filter-reports').click(filterReports); }); function filterReports() { var filters = { SelectedRtoId: $('#SelectedRtoId').val(), SelectedPricingPointId: $('#SelectedPricingPointId').val(), SelectedLoadTypeId: $('#SelectedLoadTypeId').val(), SelectedBlockId: $('#SelectedBlockId').val(), SelectedRevisionStatusId: $('#SelectedRevisionStatusId').val() } var dealSummaries = { SelectedItemIds: $('#SelectedItemIds').val() } var model = { ReportingFilters: filters, DealSummaries: dealSummaries } $('#selected-items select option').attr("selected", "selected"); $.ajax({ url: '@Url.Action("FilterReports")', data: model, contentType: 'application/json', dataType: 'json', success: function (data) { alert(data); } }); } </script> 

Models:

 public class DealSummaryComparisonViewModel { public ReportingFiltersViewModel ReportingFilters { get; set; } public LadderListViewModel DealSummaries { get; set; } } public class LadderListViewModel { public MultiSelectList AvailableItems { get; set; } public int[] SelectedItemIds { get; set; } public MultiSelectList SelectedItems { get; set; } } public class ReportingFiltersViewModel { public int? SelectedRtoId { get; set; } public ICollection<Rto> Rtos { get; set; } public int? SelectedPricingPointId { get; set; } public ICollection<PricingPoint> PricingPoints { get; set; } public int? SelectedLoadTypeId { get; set; } public ICollection<LoadType> LoadTypes { get; set; } public int? SelectedBlockId { get; set; } public ICollection<Block> Blocks { get; set; } public int? SelectedRevisionStatusId { get; set; } public ICollection<RevisionStatus> RevisionStatuses { get; set; } public bool? DealStatus { get; set; } } 

The model looks good on the client side:

 {"ReportingFilters":{ "SelectedRtoId":"5", "SelectedPricingPointId":"20", "SelectedLoadTypeId":"55", "SelectedBlockId":"21", "SelectedRevisionStatusId":"11" },"DealSummaries":{ "SelectedItemIds":["21","22","23","24","25"] }} 

So why am I not getting anything from the controller? This has been causing me trouble over the last two days, so please help! Thanks!!

UPDATE I have updated my javascript section to what I am currently using. This section now returns the model to the controller with the ReportingFilers and DealSummaries objects, but all the values โ€‹โ€‹inside are null.

Could this have anything to do with the values โ€‹โ€‹being strings? If so, how can I fix this?

+6
source share
5 answers

Change the string $ .getJSON to:

 $.ajax({ url: '@Url.Action("FilterReports")', data: JSON.stringify(viewModel), contentType: 'application/json', dataType: 'json', success: function (data) { alert(data); } }); 

This way MVC knows that it receives JSON and will correctly bind it to your model.

+4
source

Here are a few different things you could try:

  • Apparently, you should not use NULL properties in your objects if you want to use the DefaultModelBinder : ASP.NET MVC3 JSON model binding with a nested class . So you could try to make your ints non-null, or if that is not an option, do IModelBinder yourself?

  • Have you tagged your classes with SerializableAttribute ?

  • Try setting the type parameter in the ajax method to "POST" - it will use "GET" by default. type: 'POST'

  • Try setting the contentType parameter in the ajax method explicitly for this instead ... contentType: 'application/json; charset=utf-8' contentType: 'application/json; charset=utf-8'

  • And finally, are you definitely using MVC 3 rather than MVC 2? I ask because MVC 3 has a JsonValueProviderFactory baked in a framework where MVC 2 is not the case if you used MVC 2, which could explain your problem ...

+3
source

Ok, replace:

 { model: JSON.stringify(viewModel) } 

from

 { model: viewModel } 

You mix objects with JSON strings, so jQuery will be JSON.stringify of the whole object. Which will encode viewModel twice.

+2
source

here is what i would suggest, in your controller the action method should look like this:

 public JsonResult FilterAction(string model) { var viewModel=new JavaScriptSerializer().Deserialize<DealSummaryComparisonViewModel>(model); } 

Also, make sure your request reaches the desired action and look at Firebug for it.

+1
source

Try the following:

 var filters = new Object(); filters.SelectedRtoId = $('#SelectedRtoId').val(); filters.SelectedPricingPointId = $('#SelectedPricingPointId').val(); filters.SelectedLoadTypeId = $('#SelectedLoadTypeId').val(); filters.SelectedBlockId = $('#SelectedBlockId').val(); filters.SelectedRevisionStatusId = $('#SelectedRevisionStatusId').val(); var dealSummaries = new Object(); dealSummarties.SelectedItemIds = $('#SelectedItemIds').val(); var viewModel = new Object(); viewModel.ReportingFilters = filters; viewModel.DealSummaries = dealSummaries; $('#selected-items select option').attr("selected", "selected"); $.getJSON('@Url.Action("FilterReports")', { model: JSON.stringify(viewModel) }, function (data) { alert(data); }); 
0
source

Source: https://habr.com/ru/post/898284/


All Articles