我有一个ASP.NET MVC Razor
项目,DropDownList
其中有来自 的数据Database
,我试图将所选值插入DropDownList
到,Database
但我不能。
<div class="editor-label">
@Html.LabelFor(model => model.Parent_ID)
</div>
<div class="editor-field create-Bt3">
@Html.DropDownList("-Select Parent-", new SelectList(ViewBag.Parent, "Value", "Text"),"- Select Parent -")
</div>
public ActionResult Create()
{
var data = db.Categories.ToList().Distinct();
List<SelectListItem> items = new List<SelectListItem>();
foreach (var t in data)
{
SelectListItem s = new SelectListItem();
s.Text = t.Name;
s.Value = t.Cat_ID.ToString();
items.Add(s);
}
ViewBag.Parent = items;
return View();
}
[HttpPost]
public ActionResult Create(Category category, IEnumerable<HttpPostedFileBase> files)
{
if (Request.Files.Count > 0)
{
var uploadedFile = Request.Files[0];
var fileSavePath = "";
var fileName = "";
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/App_Data/Uploads/" + fileName);
uploadedFile.SaveAs(fileSavePath);
category.Path = "~/App_Data/Uploads/" + fileName;
}
category.Add_Date = DateTime.Now;
category.Last_Update = DateTime.Now;
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}