简体   繁体   中英

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?

尝试将模型中的属性公开。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM