2

ビューに List を渡しています。ビューでは、そのリストを返す必要があります。編集可能なチェック ボックスが含まれており、これは私が本当に必要としている項目です。表示される他のすべてのフィールドは、読み取り専用です。ユーザーがアイテムをクリアランスとして指定する場合は、isClearance チェックボックスをオンにします。そのため、保存ボタンを押すと、HttpPost Index() にヒットします。ただし、null を返します。List ではなく、単なる modelObject に変更すると、問題なく動作します。

コントローラーのインデックスは次のとおりです。

 public ActionResult Index()
    {
        List<ClearanceViewModel> cc = new List<ClearanceViewModel>();
        ClearanceViewModel c = new ClearanceViewModel();
        c.sku = "123";
        c.title = "Test1";
        c.includeOnSite = true;
        c.productID = 123;
        c.salePrice = Convert.ToDecimal(2.99);
        c.RetailPrice = Convert.ToDecimal(4.99);
        c.isClearance = false;
        cc.Add(c);

        c.sku = "123";
        c.title = "Test1";
        c.includeOnSite = true;
        c.productID = 123;
        c.salePrice = Convert.ToDecimal(2.99);
        c.RetailPrice = Convert.ToDecimal(4.99);
        c.isClearance = false;
        cc.Add(c);

        return View(cc);
    }

ビューは次のとおりです。

@model List<PMS.Models.ClearanceViewModel>
@{    
ViewBag.title = "Clearance List";    
}
<fieldset>
<legend><span>Clearance List</span></legend>
@using (Html.BeginForm())
{
if (Model.Count() > 0)
{    
    <table class="list">
        <tr> 
            <th>Sku</th>
            <th>Title</th>
            <th>Include On Site</th>
            <th>Sale price</th>
            <th>Retail Price</th>
            <th>Quantity</th>
            <th>Clearance</th>
        </tr>
        @foreach(var item in Model)
        {
            @Html.HiddenFor(modelItem => item.productID);
            <tr>    
                <td>@Html.DisplayFor(modelItem => item.sku)</td>
                <td>@Html.DisplayFor(modelItem => item.title)</td>
                <td>@Html.DisplayFor(modelItem => item.includeOnSite)</td>
                <td>@Html.DisplayFor(modelItem => item.salePrice)</td>
                <td>@Html.DisplayFor(modelItem => item.RetailPrice)</td>
                <td>@Html.DisplayFor(modelItem => item.quantity)</td>
                <td>@Html.EditorFor(modelItem => item.isClearance)</td>        
            </tr>
        }
    </table>
   <p>
            <input type="submit" value="Save" />
    </p>

}
}
</fieldset>

コントローラーの HttpPost は次のとおりです (clearanceModel は、アイテムのリストが必要な場合に foreach ループに到達すると null になります)。

        [HttpPost]
    public ActionResult Index(List<ClearanceViewModel> clearanceModel)
    {
        foreach (ClearanceViewModel item in clearanceModel)
        {
            if (item.isClearance == true)
            {
                // get the product object, so we can add it to the product Promotion of                      clearance
                var product = _unitOfWork.ProductRepository.Get(filter: p => p.productID == item.productID).FirstOrDefault();

                // Make sure that it isn't already in the product promotion for clearance
                var productPromotion = _unitOfWork.ProductPromotionRepository.Get(filter: pp => pp.productID == product.productID && pp.promotion.Name == "Clearance").FirstOrDefault();

                //add the product promotion
                if (productPromotion == null)
                {
                    // get the clearance promotion
                    var promotion = _unitOfWork.PromotionRepository.Get(filter: pr => pr.Name == "Clearance").FirstOrDefault();
                    if (promotion != null)
                    {
                        ProductPromotion promo = new ProductPromotion();
                        promo.productID = product.productID;
                        promo.promotionID = promotion.promotionID;
                        promo.onDate = DateTime.Now;
                        promo.offDate = null;
                        promo.canOverwrite = true;
                        _unitOfWork.ProductPromotionRepository.Create(promo);
                        _unitOfWork.SaveChanges();
                    }
                }
            }
        }
4

2 に答える 2

6

モデル バインディングは foreach ループでは機能しません。for ループを使用する必要があります。このように、インデクサー[i]を使用して、生成された html 内の各入力要素に一意の名前プロパティを作成します。

@for (int i = 0; i < Model.Count; i++)
        {
            @Html.HiddenFor(modelItem => modelItem[i].productID);
            <tr>    
                <td>@Html.DisplayFor(modelItem => modelItem[i].sku)</td>
                <td>@Html.DisplayFor(modelItem => modelItem[i].title)</td>
                <td>@Html.DisplayFor(modelItem => modelItem[i].includeOnSite)</td>
                <td>@Html.DisplayFor(modelItem => modelItem[i].salePrice)</td>
                <td>@Html.DisplayFor(modelItem => modelItem[i].RetailPrice)</td>
                <td>@Html.DisplayFor(modelItem => modelItem[i].quantity)</td>
                <td>@Html.EditorFor(modelItem => modelItem[i].isClearance)</td>        
            </tr>
        }

モデル バインディング コレクションのわかりやすい説明については、この記事をご覧ください。

于 2013-01-04T19:19:33.757 に答える
0

リストをバインドするには、モデル バインダー、または displayTemplate が必要です。現在、生成されたリスト アイテムの名前は、item.sku、item.title などです。標準のモデルバインダーは、それをリストにバインドできません。

于 2013-01-04T19:13:19.573 に答える