Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

c# - Deserialization / model binding in MVC4 webapi does not work with arrays

I'm using the new WebApi which is part of MVC4 beta. I have the following class:

public class Voucher
{
    public string Id { get; set; }
    public string TableId { get; set; }
    public Product[] Products { get; set; } 
}

My controller looks like this:

public class VouchersController : ApiController
{
    public Voucher PostVoucher(Voucher voucher)
    {
          //....
    }
}

On the client side I serialize the data using an XmlSerializer. The output looks like expected and the Products array is serialized. If I post the data and put a break point inside the PostVoucher method, I get the data for Id and TableId, but Products is null. Any hint what I might be doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is an issue with the model binding / deserialization of complex types and XML in the ASP.NET Web API Beta. One way to solve this issue is to "disable" model binding, and select the "deserialization path" for this action. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/27/disabling-model-binding-on-asp-net-web-apis-beta.aspx shows how to do that.

Notice that this is a known issue for beta, and will be fixed in the next (RC) release.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...