在ASP.NET MVC中填充DropDownList

本文关键字:填充 DropDownList MVC ASP NET | 更新日期: 2023-09-27 18:04:15

所以基本上我想做一个网站,可以做一些像超市里的程序之类的事情包含卖家工作的产品/视图的数据库、添加新产品的视图和收据模型。所以我有一个类

public class Product
{
    public int ID { get; set; }
    public string Title { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}
public class ProduktiDBContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

和控制器

public class ProduktiController : Controller
{
    private ProduktiDBContext db = new ProduktiDBContext();
    public ActionResult Index()
    {
        return View(db.Products.ToList());
    }
    public ActionResult Create()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Create(Product product)
    {
        if (ModelState.IsValid)
        {
            db.Products.Add(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(product);
    }
}

在索引视图中,我想添加一个DropDownList,其中包含数据库中所有产品的标题

@model IEnumerable<Produkti.Models.Product>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Quantity)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Quantity)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

<hr />
    @foreach (var item in Model) {
    <tr>
        <td>
            //I'd like to put the dropdownlist here i dont know if this for loop is necessary
        </td>
       <td>
       </td>
    </tr>
}
</table>

我想知道我是否需要将一个包含所有标题的列表从控制器传递到视图,或者索引方法中的return View(db.Products.ToList());已经传递了我需要的数据,以及如何从这个DropDownList 传递数据

在ASP.NET MVC中填充DropDownList

我建议创建一个包含List<Product>List<SelectListItem>的新模型类,而不是使用IEnumerable<Produkti.Models.Product>作为模型。假设类名是ProductModel,下面是类定义的

public class ProductModel
{
    public ProductModel()
    {
        this.Products = new List<Product>();
        this.ProductsDropdownItems = new List<SelectListItem>();
    }
    public List<Product> Products { get; set; }
    public int SelectedProductID { get; set; }
    public List<SelectListItem> ProductsDropdownItems { get; set; }
}

将控制器的操作方法更改为此

public class ProduktiController : Controller
{
    private ProduktiDBContext db = new ProduktiDBContext();
    public ActionResult Index()
    {
        ProductModel model = new ProductModel();
        model.Products = db.Products.ToList();
        foreach(var p in db.Products)
        {
            model.ProductsDropdownItems.Add(new SelectListItem() { Text = p.Title, Value = p.ID.ToString() });
        }
        return View(model);
    }
}

然后更改您的视图代码

@model Produkti.Models.ProductModel
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Title
        </th>
        <th>
            Quantity
        </th>
        <th>
            Price
        </th>
    </tr>
@foreach (var item in Model.Products) {
    <tr>
        <td>
            @item.Title
        </td>
        <td>
            @item.Quantity
        </td>
        <td>
            @item.Price
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

<hr />
    <tr>
        <td>
            @Html.DropDownListFor(m => m.SelectedProductID, Model.ProductsDropdownItems)
        </td>
       <td>
       </td>
    </tr>
</table>

向您的模型添加列表

public List<SelectListItem> Products { get; set; }

然后在您的控制器上,填充该列表

foreach(var temp in db.Products){
    model.Products.Add(new SelectListItem() { Text = temp.ProductName, Value = temp.ProductID });
}

然后在你的视图

@Html.DropDownListFor(x => x.SelectedProduct, model.Products)

您可以通过设置模型来设置下拉列表。获取上的SelectedProduct,该值将在后用选定值填充

方法1:

在你的行动中这样做:

public ActionResult YourAction()
{
ViewBag.DDLProducts = new SelectList(db.Products,"ProductID","ProductName");
return View();
}

视图中:

@Html.DropDownListFor(model => model.SelectedProduct,  (List<SelectListItem>)ViewBag.DDLProducts, "Select One")

方法2:

另一种方法是,如果你不需要改变行动或模式:

public ActionResult YourAction()
    {

    return View();
    }

视图中:

@Html.DropDownListFor(model => model.SelectedProduct,new SelectList(db.Products,"ProductID","ProductName"), "Select One")
public ActionResult Index()
 {
  ViewBag.Products = new SelectList(db.Products,"ProductID","ProductName");
  return View();
  }

在视图中只需编写声明。

@Html.DropDownList("Products");