0

I have a ASP.NET MVC wrapper class that wraps a List containing ViewModels:

public class ScheduleContainerViewModel
{
    public List<SchedulingViewModel> Items { get; set; }

    public ScheduleContainerViewModel(List<SchedulingViewModel> items)
    {
        Items = items;
}

SchedulingViewModel contains a Schedule object and a Users object.

ScheduleContainerViewModel is eventually passed to a PartialView. This PartialView is called via Ajax to populate a div in a View. The model for the PartialView is ScheduleContainerViewModel and the model for the View is a Job.

The Ajax GET works great. When I try to POST it, however, the Controller is never called and I get a 500 server error. Here's some of the XHR data being POSTed: enter image description here

It seems to me that List Items is being passed, containing SchedulingViewModels and their associated Schedules, but maybe not?

There are three relevant methods in the Controller. Here are their signatures:

public ActionResult ScheduleJob(int? id)
public ActionResult Table(int? id)
[HttpPost]
public ActionResult Table(ScheduleContainerViewModel cvm)

ScheduleJob loads the initial View, which calls [HttpGet] Table to load the PartialView. Finally, [HttpPost] Table is the one being POSTed to. Why can't the model binder see the data being passed to it in the XHR?

My only guess it that it's because the model used by the View called by ScheduleJob is different than the one being POSTed to [HttpPost] Table.

Harry T
  • 131
  • 2
  • 12
  • _I get a 500 server error_ What is this error? You should be able to hit the `Response` tab in the above screenshot to view it or simply debug the post in Visual Studio. _My only guess it that it's because the model used by the View called by ScheduleJob is different than the one being POSTed to_ - Doesn't matter. – zgood May 19 '17 at 20:18
  • It's an HTML error page with the error `System.MissingMethodException: No parameterless constructor defined for this object.`, for the `SchedulingViewModel`. Would ASP know how to populate a `SchedulingViewModel` with no constructor just given the XHR data above? – Harry T May 19 '17 at 20:24
  • [Check this out](http://stackoverflow.com/questions/1355464/asp-net-mvc-no-parameterless-constructor-defined-for-this-object) it appears to be the problem you are having. on your `ScheduleContainerViewModel` you need a parameterless constructor. i.e. `public ScheduleContainerViewModel() { }` – zgood May 19 '17 at 20:30
  • I'll answer my own question to say: yes, it can. Thank you! – Harry T May 19 '17 at 20:31

0 Answers0