如何在asp.net mvc中使用dropdownlist在数据库中插入记录?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在asp.net mvc中使用dropdownlist在数据库中插入记录?相关的知识,希望对你有一定的参考价值。
我在asp.net MVC中使用实体框架数据库优先方法实现下拉列表?
我在MVC中实现了一个下拉列表,并且当我在监视窗口中调试时,显示了我插入的记录,但没有插入数据库,这是一个问题?
数据库字段名称:
tbl_product
- ProductID int
- ProductName varchar(50)
- ProductList varchar(50)
tbl_product.cs
// <auto-generated>
// </auto-generated>
public partial class tbl_product
public int ProductID get; set;
public string ProductName get; set;
public string ProductList get; set;
public IEnumerable<SelectListItem> ProductLists get; set;
HomeController.cs
ProductEntities prodb = new ProductEntities();
public ActionResult Index()
return View(prodb.tbl_product.ToList());
public ActionResult Prodlist()
var ProductLists = GetallProductList();
var model = new tbl_product();
model.ProductLists = GetSelectListItems(ProductLists);
return View(model);
[HttpPost]
public ActionResult Prodlist(tbl_product product)
var ProductLists = GetallProductList();
product.ProductLists = GetSelectListItems(ProductLists);
if (ModelState.IsValid)
prodb.tbl_product.Add(product);
return RedirectToAction("Index");
return View("Prodlist", product);
private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<string> elements)
var selectList = new List<SelectListItem>();
foreach (var element in elements)
selectList.Add(new SelectListItem
Value = element,
Text = element
);
return selectList;
private IEnumerable<string> GetallProductList()
return new List<string>
"FRIDGE",
"WASHING MACHINE",
"A.C",
;
视图:
Prodlist.cshtml
<div class="form-group">
@html.LabelFor(model => model.ProductName, htmlAttributes: new @class = "control-label col-md-2" )
<div>
@Html.EditorFor(model => model.ProductName, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.ProductName, "", new @class = "text-danger" )
</div>
</div>
//product list
<div class="form-group">
@Html.LabelFor(model => model.ProductList, htmlAttributes: new @class = "control-label col-md-2" )
<div>
@Html.DropDownListFor(model => model.ProductList,Model.ProductLists,"Please Select Your Products:", new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.ProductList, "", new @class = "text-danger" )
</div>
</div>
Model1.Context.cs
// <auto-generated>
// </auto-generated>
public partial class ProductEntities : DbContext
public ProductEntities()
: base("name=ProductEntities")
public virtual DbSet<tbl_product> tbl_product get; set;
Index.cshtml
@model IEnumerable<DropdownInsertMvc.Models.tbl_product>
@
ViewBag.Title = "Index";
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.ProductList)
</th>
<th></th>
</tr>
@foreach (var item in Model)
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductList)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new id=item.ProductID ) |
@Html.ActionLink("Details", "Details", new id=item.ProductID ) |
@Html.ActionLink("Delete", "Delete", new id=item.ProductID )
</td>
</tr>
</table>
当我在监视窗口中调试时:
- ProductList“ FRIDGE”字符串
- [ProductLists Count = 3 System.Collections.Generic.IEnumerable System.Collections.Generic.List] >>
- 产品名称“电子”字符串
记录未插入数据库中?
我将在asp.net MVC中使用实体框架数据库优先方法实现下拉列表?我在MVC中实现了一个下拉列表,并且在调试时在监视窗口中显示了我插入的记录...
答案
您在prodb.SaveChanges()
方法中的prodb.tbl_product.Add(product);
之后错过了Prodlist
。
以上是关于如何在asp.net mvc中使用dropdownlist在数据库中插入记录?的主要内容,如果未能解决你的问题,请参考以下文章
Asp.Net MVC中DropDownListFor的用法(转)
使用 Url.Action (ASP.NET MVC) 将分号附加到 href URL
ASP.NET DropDown SelectedIndexChanged 未在 Firefox 中使用 UpdatePanel 触发