1

私はASP.NET MVC でこのチュートリアルに"MovieType"従っています。具体的には、次のように Razor View にString を提供すると記載され@Html.DropDownList("MovieType")ます。ViewBagIEnumerable<SelectListItem>

それはうまくいきます。

ただし、[HttpPost]属性を使用してメソッドでユーザーが選択した値を取得できません。

ここに私のコードと私がこれまでに試したことがあります。

コントローラ

public class ProductController : Controller 
{
    private readonly ProductRepository repository = new ProductRepository();

    // Get: /Product/Create
    public ActionResult Create()
    {
        var categories = repository.FindAllCategories(false);
        var categorySelectListItems = from cat in categories
                                      select new SelectListItem()
                                          {
                                              Text = cat.CategoryName,
                                              Value = cat.Id.ToString()
                                          };
        ViewBag.ListItems = categorySelectListItems;
        return View();
    }

    [HttpPost]
    public ActionResult Create(Product product)
    {
        /** The following line is not getting back the selected Category ID **/
        var selectedCategoryId = ViewBag.ListItems;
        repository.SaveProduct(product);
        return RedirectToAction("Index");
    }
}

Razor cshtml ビュー

@model Store.Models.Product

<h2>Create a new Product</h2>

@using (@Html.BeginForm())
{
    <p>Product Name:</p>
    @Html.TextBoxFor(m => m.ProductName)

    <p>Price</p>
    @Html.TextBoxFor(m => m.Price)

    <p>Quantity</p>
    @Html.TextBoxFor(m => m.Quantity)

    <p>Category</p>
    @Html.DropDownList("ListItems")
    <p><input type="submit" value="Create New Product"/></p>
}

のオーバーロードを試してみましたDropDownListが、ユーザーが選択している値を取り戻すことができません。

誰かが私に欠けているものを見たり、アイデアや提案があれば、とても感謝しています. ありがとう!

アップデート

投稿Productモデル。これは、.edmx を作成したときに Entity Framework によって自動生成されたことに注意してください。

namespace Store.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Product
    {
        public long Id { get; set; }
        public string ProductName { get; set; }
        public decimal Price { get; set; }
        public int Quantity { get; set; }
        public System.DateTime DateAdded { get; set; }
        public Nullable<long> CategoryId { get; set; }

        public virtual Category Category { get; set; }
    }
}
4

3 に答える 3

6

タイプのモデルをビューに渡していStore.Models.Productます。他のフィールドは、ラムダ式を使用してこのモデルに@Html.DropDownList()バインドされていますが、モデルにバインドされていないため、HTTPPost でモデルが返されたときに選択がありません。

Categoryフィールドを追加してからStore.Models.Product、次を使用してそのリストにバインドする必要があります。

@Html.DropDownListFor(m => m.Category, ViewBag.ListItems)

次に、MVC は入力コントロールをモデルに適切にバインドする必要があります。

例として、次の構文は機能します。

int[] listItems = {1, 2, 3, 4};
SelectList selectList = new SelectList(listItems);
@Html.DropDownListFor(m => m.CategoryId, selectList);

which implementsSelectListから継承することに注意してください。MultiSelectListIEnumerable<SelectListItem>

于 2013-03-29T19:08:08.443 に答える
2

Peter G. が彼の回答で述べたことに従いますが、情報提供のみを目的として、Post コントローラーを次のように変更することもできます。

[HttpPost]
public ActionResult Create(Product product, int ListItems)
{

}

ピーターが述べたように (もう一度、彼の回答を使用してください)、問題は、フォームが投稿されたときに、ドロップダウン リストに「ListItems」という名前が付けられ、コントローラーにバインドするものが何もないことです。

于 2013-03-29T19:14:59.280 に答える
0

私は最終的に次のことをしました:

コントローラ

...
    [HttpPost]
    public ActionResult Create(Product product)
    {
        var categoryId = product.CategoryId;
        repository.SaveProduct(product);
        return RedirectToAction("Index");
    }
...

Razor cshtml ビュー

...
<p>Category</p>
@Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>) ViewData["ListItems"])
... 

それは見苦しく、正直に言うと、ピーターが提案したようにラムダ式を使用できなかった理由を完全には理解していません@Html.DropDownListFor(m => m.CategoryId, ViewBag.ListItems).

私には「うまくいきました」が、その理由を理解したいと思っています。もっとエレガントな方法があれば、それを行うことができます。

と入力すると@Html.DropDownListFor(m => m.CategoryId, ViewBag.ListItems)、IntelliSense で が自動入力されm.CategoryIdますが、赤でフラグが付けられ、実行時エラーが発生します。

于 2013-03-29T19:29:07.343 に答える