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