2009-03-05 2 views
5

私はASP.NET MVCのRC1を使用しています。リストを含む既定のモデルバインダーと複合型

モデルバインディングの例をPhil Haack's拡張しようとしています。

コントローラー:

using System.Collections.Generic; 
using System.Web.Mvc; 

namespace TestBinding.Controllers 
{ 
    [HandleError] 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewData["Message"] = "Welcome to ASP.NET MVC!"; 

      return View(); 
     } 

     //Action method on HomeController 
     public ActionResult UpdateProducts(ListOfProducts productlist) 
     { 
      return View(productlist); 
     } 
    } 

    public class Product 
    { 
     public string Name { get; set; } 
     public decimal Price { get; set; } 
    } 

    public class ListOfProducts 
    { 
     public int Id { get; set; } 
     public string Title { get; set; } 
     List<Product> Items { get; set; } 
    } 
} 

ビュー私はいくつかの変更でフィルの例からコードを使用してい

public class ListOfProducts 
{ 
    public int Id { get; set; } 
    public string Title{ get; set; } 
    List<Product> Items { get; set; } 
} 

public class Product 
{ 
    public string Name { get; set; } 
    public decimal Price { get; set; } 
} 

:私は、次のオブジェクトをバインドするために、デフォルトのモデルバインダーを使用しようとしています:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 

    <asp:Content ID="indexHead" ContentPlaceHolderID="head" runat="server"> 
     <title>Home Page</title> 
    </asp:Content> 

    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> 
     <form method="post" action="/Home/UpdateProducts"> 
      <input type="text" name="productlist.id" value="99" /> 
      <input type="text" name="productlist.Title" value="SomeTitle" /> 

      <input type="hidden" name="productlist.Index" value="0" /> 
      <input type="text" name="productlist.items[0].Name" value="Beer" /> 
      <input type="text" name="productlist.items[0].Price" value="7.32" /> 

      <input type="hidden" name="productlist.Index" value="1" /> 
      <input type="text" name="productlist.Items[1].Name" value="Chips" /> 
      <input type="text" name="productlist.Items[1].Price" value="2.23" /> 

      <input type="hidden" name="productlist.Index" value="2" /> 
      <input type="text" name="productlist.Items[2].Name" value="Salsa" /> 
      <input type="text" name="productlist.Items[2].Price" value="1.23" /> 

      <input type="submit" /> 
     </form> 
    </asp:Content> 

私の問題は、単純なタイプ(IdとTitle)がproductlist objeに表示されるということですctが、リストは表示されません。したがって:

  • 私のコードは悪いですか(驚くことはありません)?
  • デフォルトのモデルバインダーはListOfProductsオブジェクトを処理できますか?
  • デフォルトのモデルバインダーがこのタイプのオブジェクトを処理しない場合、何が必要なのですか(可能な場合の例)?

ありがとうございます。 RC 1以降

答えて

6

私自身の質問にお答えします:

私はダミーです!

ListOfProductsクラスのItemsプロパティが公開されていないので、私の例では動作しません:

public class ListOfProducts 
{ 
    public int Id { get; set; } 
    public string Title{ get; set; } 
    List<Product> Items { get; set; } 
} 

は私が変更されました:

List<Product> Items { get; set; } 

へ:

public List<Product> Items { get; set; } 

と私コードが働いた。

結論として、デフォルトのモデルバインダーは、List型のプロパティを含む型で機能します。

+3

あなたはダミーではありません。私は同じ問題を抱えていましたが、私のリストは財産ではなかったからです。私は次のようなものを持っていました:公開リスト公開リストではなく、アイテムリストアイテム{get;セット; } –

4

  • 隠し指数はもはや[] 0と必見上昇で開始する必要がありますに
  • 数を必要としません。

番号は問題ありません。

また、アイテムのプロパティ名に異なるケーシングを使用していたことに気付きました。違いはありませんが、チェックする価値があります。

+1

こんにちはクレイグ、ヒントをいただきありがとうございました。 –

+1

+1保存した私の日 – alexandrul

関連する問題