Я нахожу, как создать раскрывающийся список, но знаю, что языки и технологии не сохраняются в моей базе данных. Может кто-нибудь знает, почему это так?
ProjectController.cs
public ActionResult Create()
{
using (ApplicationDbContext pvEntity = new ApplicationDbContext())
{
var fromDatabaseEF = new SelectList(pvEntity.Languages.ToList(), "Price", "Name");
ViewData["Languages"] = fromDatabaseEF;
}
using (ApplicationDbContext pvEntity = new ApplicationDbContext())
{
var fromDatabaseEF = new SelectList(pvEntity.Technologies.ToList(), "Price", "Name");
ViewData["Technologies"] = fromDatabaseEF;
}
return View();
}
// POST: Projects/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,ProjectName,HoursEntity,Description,Language,Technology,Cost")] Project project)
{
//ViewBag.Languages = new SelectList(db.Languages, "Name", "Price", project.Languages);
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
Просмотр проекта
div class="form-group">
@Html.LabelFor(model => model.Language, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Languages", (IEnumerable<SelectListItem>)ViewData["Languages"],"Choose Languages",new { @class = "form-control" } )
@Html.ValidationMessageFor(model => model.Language, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Technology, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.Technology, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.DropDownList("Technologies", (IEnumerable<SelectListItem>)ViewData["Technologies"],"Choose Technology", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Technology, "", new { @class = "text-danger" })
</div>
</div>