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