ASP.NET MVC-Modell, das eine IList & lt; & gt; Parameter

[Löste ich dieses selber, siehe meine Antwort für die Ursache]

Ich habe Probleme dabei bilden Werte, die für eine IList<> argument in einer controller-Methode richtig eingestellt.

Meine controller-Klasse sieht wie folgt aus:

public class ShoppingBasketController : Controller {

    public ActionResult Index() {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Add(IList<ShoppingBasketItem> items) {
        Session["basket"] = items; //for testing
        return RedirectToAction("Index");
    }
}
public class ShoppingBasketItem {
     public int ItemID;
     public int ItemQuantity;
}

Leicht getrimmt form:

<% using (Html.BeginForm("Add", "ShoppingBasket")) { %>
    <% int codeIndex = 0;
    foreach (Product product in products) { %>
        <%= Html.Hidden("items[" + codeIndex + "].ItemID", product.Id) %>
        <%= Html.TextBox("items[" + codeIndex + "].ItemQuantity", "0", new { size = "2"}) %>
        <% codeIndex++;
    }
} %>

Erzeugt markup wie:

<form action="/Basket/Add" method="post">
    <input id="items[0]_ItemID" name="items[0].ItemID" type="hidden" value="1" />
    <input id="items[0]_ItemQuantity" name="items[0].ItemQuantity" size="2" type="text" value="0" />

    <input id="items[1]_ItemID" name="items[1].ItemID" type="hidden" value="2" />
    <input id="items[1]_ItemQuantity" name="items[2].ItemQuantity" size="2" type="text" value="0" />

    <input id="items[2]_ItemID" name="items[2].ItemID" type="hidden" value="3" />
    <input id="items[2]_ItemQuantity" name="items[2].ItemQuantity" size="2" type="text" value="0" />
</form>

Habe ich überprüft das Formular Werte, die Sie vorgelegt bekommen und Sie sind richtig. Die richtige Anzahl von ShoppingBasketItems auch bekommen in Session["basket"], jedoch sowohl der ItemID und ItemQuantity sind null. Es erscheint korrekt Dekodieren der Liste der Formular-Werte, aber nicht die Abholung der Eigenschaften selbst.

Ich bin mit MVC-RC2-basierend auf einem Artikel von Scott Hanselman ich bin mir ziemlich sicher, dass mein code korrekt ist. Bin ich etwas fehlt?

Kommentar zu dem Problem
Gibt es eine Möglichkeit, um diese Arbeit mit UI-templates, mit einem "EditorFor" (m => m ist.Produkt), die für jede iteration in Produkten? Kommentarautor: Zachary Scott
Danke. Ich fand diese sehr hilfreich. 🙂 Kommentarautor: Ryan Sampson

InformationsquelleAutor der Frage roryf | 2009-03-17

Schreibe einen Kommentar