gpt4 book ai didi

c# - 在 ASP.NET MVC 中填充 DropDownList

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:09 26 4
gpt4 key购买 nike

所以基本上我正在尝试制作一个网站,该网站可以执行类似于超市中的程序的操作包含产品的数据库/卖家工作的 View ,添加新产品的 View 和收据模型。所以我有一个类

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; }
}

和一个 Controller

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);
}
}

在索引 View 中,我想添加一个 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>

我想知道我是否需要将包含所有标题的列表从 Controller 传递到 View ,或者 return View(db.Products.ToList()); 来自 Index 方法已经通过我需要的数据,以及如何从这个 DropDownList 传递数据

最佳答案

而不是使用 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; }
}

将您的 Controller 操作方法更改为此

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);
}
}

然后更改您的 View 代码

@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>

关于c# - 在 ASP.NET MVC 中填充 DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23773085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com