在ASP.NET MVC中填充DropDownList

时间:2014-05-21 02:37:17

标签: c# asp.net asp.net-mvc asp.net-mvc-4

所以基本上我正在努力创建一个可以像超市中的程序那样做的网站 带产品的数据库/卖家工作的视图,添加新产品的视图和收据笔记模型。所以我有一个班级

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>

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

4 个答案:

答案 0 :(得分:1)

向模型添加列表

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)

您可以通过设置模型来设置下拉列表。在get和该值上将使用选定的值填充该值

答案 1 :(得分:1)

我建议您创建一个包含IEnumerable<Produkti.Models.Product>List<Product>的新模型类,而不是使用List<SelectListItem>作为模型。我们假设类名是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>

答案 2 :(得分:0)

方法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")

答案 3 :(得分:0)

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

在视图中写下声明。

@Html.DropDownList("Products");
相关问题