0

This is a slightly original variation on a common problem. I have a fairly simple model that looks like this:

public class Departure
{
    public string Id { get; set; }
    public Route Route { get; set; }
    public TimeSpan Time { get; set; }
    public string Timetable { get; set; }
    public DayOfWeek[] ServicesRunning { get; set; }
    public string Notes { get; set; }
}

I am posting a populated object back to the server but populating the array of DayOfWeek values is problematic. Integer values and strings aren't being correctly recognised so there must be a particular technique for doing this correctly but I haven't been able to work it out.

Any assistance would be much appreciated. The post method looks like this:

    // POST /api/stops
    [HttpPost]
    public void Post(Departure dep)
    {
        Session.Store(dep);
    }
Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155

2 Answers2

0

You can write a ModelBinder to set the DayOfTheWeek enum values from the integers that are passed in via AJAX. Check out this answer for more details.

Model Binding to Enums in ASP.NET MVC 3

Community
  • 1
  • 1
Scorpion-Prince
  • 3,574
  • 3
  • 18
  • 24
0

The following:

$.ajax({
    url: '@Url.Action("post")',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({
        dep: {
            servicesRunning: [ 'Monday', 'Tuesday', 'Wednesday' ]
        }
    }),
    success: function () {

    }
});

work fine for me.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928