Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Web API model binding non-sequential list of complex objects
    primarykey
    data
    text
    <p>I am attempting to model bind a complex object with a non-sequential list using an ApiController. All of the fields except the list are set correctly, but the list contains one element (even though two list elements were posted) and the element is null. If I take the exact same code and point it to an MVC Controller using the same parameter type in my action method, everything works as expected.</p> <p>Since I am using a non-sequential list, I am using the hidden ".Index" input as described by Phil Haack (<a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" rel="nofollow">http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx</a>)</p> <p>The ApiController also binds the list correctly if I remove the ".Index" input and send the list as a sequential list starting at 0. (This option work for testing, but is not a great option in production as the list items can be added and removed by the user, which is why I want to use the non-sequential list.)</p> <p>I understand that Web API Controllers do parameter binding differently than MVC Controllers as discussed <a href="http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx/" rel="nofollow">here</a>, but it seems like non-sequential lists should bind correctly in Web API Controllers. Am I missing something? Why does the same code work for an MVC Controller and not a Web API Controller? How can I get non-sequential lists to bind correctly in Web API?</p> <p><strong>Here are my Post parameters:</strong></p> <pre><code>Parameters application/x-www-form-urlencoded BatchProductLots.Index 1 BatchProductLots.Index 2 BatchProductLots[1].BrandId 1 BatchProductLots[1].ContainerId 9 BatchProductLots[1].ContainerLot 123 BatchProductLots[1].PackageId 2 BatchProductLots[1].PlannedQuantity 0 BatchProductLots[1].ProducedQuantity 20 BatchProductLots[2].BrandId 1 BatchProductLots[2].ContainerId 9 BatchProductLots[2].ContainerLot 123 BatchProductLots[2].PackageId 1 BatchProductLots[2].PlannedQuantity 0 BatchProductLots[2].ProducedQuantity 1 BatchStatusId 1 LotNumber 070313 ProductionDate 07/03/2013 RecipeId 1 RecipeQuantity 1 SauceId 22 X-Requested-With XMLHttpRequest </code></pre> <p><strong>Here is my Web API Controller Action:</strong></p> <p><strong>(request.BatchProductLots list is set to one element (even though two elements were posted) and that one element is null)</strong></p> <pre><code>public Response Create(BatchCreateRequest request) { Response response = new Response(); try { Batch batch = Mapper.Map&lt;Batch&gt;(request); batchService.Save(batch); response.Success = true; } catch (Exception ex) { response.Message = ex.Message; response.Success = false; } return response; } </code></pre> <p><strong>Here is the complex object with the list that I am attempting to bind to:</strong></p> <pre><code>public class BatchCreateRequest { public int BatchStatusId { get; set; } public DateTime ProductionDate { get; set; } public string LotNumber { get; set; } public int SauceId { get; set; } public int RecipeId { get; set; } public int RecipeQuantity { get; set; } public List&lt;BatchProductLot&gt; BatchProductLots { get; set; } public class BatchProductLot { public int BrandId { get; set; } public int ContainerId { get; set; } public string ContainerLot { get; set; } public int PackageId { get; set; } public int PlannedQuantity { get; set; } public int ProducedQuantity { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload