dww84
dww84

Reputation: 53

Passing generic parameters into MVC Action

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?

Upvotes: 1

Views: 2014

Answers (1)

Denis Borovnev
Denis Borovnev

Reputation: 486

Try to make the properties in your model public.

Upvotes: 5

Related Questions