1

I'm building an MVC controller, which will accept a JSON object, but I want to make the type it accepts generic. Can I do this?

public class RecognitionData<T> 
{
    DateTime StartTime { get; set; }
    DateTime EndTime { get; set; }
    T Value { get; set; }
}

public class Address
{
    string Line1 { get; set; }
    string Line2 { get; set; }
    string Locality { get; set; }
    string Country { get; set; }
    string Postcode { get; set; }
}

public class AddressController : Controller
{
    public string Save(RecognitionData<Address> result)
    {
        ...
    }
}

When I try to do post the data as JSON, while I can see the "result" field in Request["result"] as a string, the "result" parameter is blank.

Any ideas what I'm doing wrong?

dww84
  • 53
  • 1
  • 7
  • 3
    Is this what you're looking for? http://stackoverflow.com/questions/1487005/asp-net-mvc-model-binder-for-generic-type –  Sep 07 '12 at 08:34

1 Answers1

5

Try to make the properties in your model public.

H H
  • 263,252
  • 30
  • 330
  • 514
Denis Borovnev
  • 486
  • 3
  • 3
  • Doh. I couldn't post my exact code so put an example of what I was trying to do. Missed making them public out of my example :) – dww84 Sep 08 '12 at 00:26
  • 1
    :) but I think the issue still not in generic model. Try to create not generic version of RecognitionData { public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public Address Value { get; set; } } and specify this as argument of the action. And if it will work the problem really in current model, otherwise - not – Denis Borovnev Sep 08 '12 at 09:13