简体   繁体   English

将通用参数传递给MVC Action

[英]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. 我正在构建一个MVC控制器,它将接受JSON对象,但我想使其接受通用类型。 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. 当我尝试将数据发布为JSON时,虽然可以在Request [“ result”]中看到“ result”字段为字符串,但“ result”参数为空。

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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