我想加载一个下拉列表,其中包含从我的 mysql 数据库接收到的值。
这就是我在ViewModel中的内容:
public int ProjectID { get; set; }
public IEnumerable<SelectListItem> Projects { get; set; }
在我的Controller中创建函数:
public ActionResult Create(DeliverableViewModel model)
{
var projects = (repository.GetProjects()).ToList();
ViewBag.Projects = projects;
if (ModelState.IsValid)
{
try
{
repository.AddDeliverable(model.ProjectID);
}
catch (ArgumentException ae)
{
ModelState.AddModelError("", ae.Message);
}
return RedirectToAction("Index");
}
return View(model);
}
我将项目加载到我的存储库中。(值:project_id 和 project_name)
这是我的创建视图:
@model GDMfrontEnd.Models.DeliverableViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Deliverable", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="editor-field">
@Html.DropDownListFor(model => model.Projects,
new SelectList(ViewBag.Projects,
"project_id", "project_name"))
</div>
<p>
<input type="submit" value="Create" />
</p>
}
但是现在,如何将我的 ProjectID 从所选值提供给我的存储库?