0

ここに私は自分のプロジェクトのドロップダウンリストを持っています.しかし、ここで私は選択した値を取得することに固執しています.

@Html.DropDownListFor(m => m.ProductType, (SelectList)ViewBag.ListOfCategories, new { @class = "form-control"})

型式コード

[Required]
        public string ProductType { get; set; }

コントローラ

 [HttpPost]
    public ActionResult AddProduct(ICS.Models.ProductsModels.Products model)
    {
        ProductController _ctrl = new ProductController();
        _ctrl.AddorUpdateProduct(new ICS.Data.Product
        {
            ProductName = model.ProductName,
            ProductType = model.ProductType,
            IsFixed = model.PriceSettings,
            ItemPrice = model.ItemPrice,
            PurchasePrice = model.PurchasePrice,
            Vat = model.Vat,
            WholeSalePrice = model.WholeSalePrice,
            Comments = model.Comments
        });
        return View(model);
    }


[HttpGet]
    public ActionResult AddProduct()
    {
        ViewBag.ListOfCategories = new SelectList(_cat.GetCategory(), "CategoryId", "CategoryName");
        return View();
    }
4

1 に答える 1

1

Razorは、テキストとは何か、ドロップダウンリストオプションで値でなければならないものを理解していないため、空のドロップダウン(値属性なし)を生成するだけであることをお勧めします。レンダリングされたhtmlを確認できます。次のように見えると思います

<select>
   <option>Category1Name</option>
   <option>Category2Name</option>
   <option>Category3Name</option>
   ...
</select>

IEnumerable<SelectListItem>ドロップダウンのソースとして使用する必要があります。例:

[HttpGet]
public ActionResult AddProduct()
{
    // this has to be the list of all categories you want to chose from
    // I'm not shure that _cat.GetCategory() method gets all categories. If it does You
    // should rename it for more readability to GetCategories() for example
    var listOfCategories = _cat.GetCategory();

    ViewBag.ListOfCategories = listOfCategories.Select(c => new SelectListItem {
        Text = c.CategoryName,
        Value = c.CategoryId
    }).ToList();

    return View();
}
于 2013-11-11T05:27:38.370 に答える