سؤال

I've searched extensively but can't figure this out having tried a number of approaches from questions\answers on stackoverflow, so asking for help.

I want to create a number of dropdowns from a table I'm accessing via EF.

In my Controller

public ActionResult GetProductsOnPreOrder()
{
    PreOrderProducts = _Db.PRODUCT_TABLE.Where(q => q.IsOnPreOrder == true)
        .OrderBy(o => o.PreOrderShippingStartDate).ToList<PRODUCT_TABLE>();

    return View(PreOrderProducts);
}

This table then contains, for example, fields called ID and Name which I want to create a dropdown for in my view (I actually need to create 6 dropdowns using various fields from this table).

Can't for the life of me figure out how to do this in my strongly typed razor view

@model IList< PRODUCT_TABLE>
هل كانت مفيدة؟

المحلول

You need to create 6 different select lists from the table, and then you need to pass them to the form, either via ViewBag or via View Model. For example:

ViewBag.SelectList1 = new SelectList(PreOrderProducts, "TextColumn1", "ValueColumn1");
ViewBag.SelectList2 = new SelectList(PreOrderProducts, "TextColumn2", "ValueColumn2");

And so on...

You can use them like:

@Html.DropDownFor(mode=>model.AnyField1, ViewBag.SelectList1 as SelectList)
@Html.DropDownFor(mode=>model.AnyField2, ViewBag.SelectList2 as SelectList)

And so on...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top