使用 selectlist 从下拉列表中保存数据

Save data from dropdownlist using selectlist

我正在尝试使用 SelectListItem 保存数据。我设法显示了数据,但是我想不出一种方法可以将选定的项目保存到数据库中。 我的控制器方法是:

public ActionResult Create()
{
    ViewBag.ProposerID = new SelectList(db.Proposers, "ID", "ProposerName");

    List<SelectListItem> projectType = new List<SelectListItem>();
    projectType.Add(new SelectListItem { Text = "Development", Value = "1" });
    projectType.Add(new SelectListItem { Text = "Research", Value = "2" , Selected = true});
    projectType.Add(new SelectListItem { Text = "Hybrid", Value = "3" });
    projectType.Add(new SelectListItem { Text = "Other", Value = "4" });
    ViewBag.ProjectType = projectType; 
    return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
 [Bind(Include = "ID,ProjectTitle,Description,ProposedDate,ProjectType,ProjectStatus,ProjectDifficulty,ProposerID")] Project project)
{
    try {
        if (ModelState.IsValid && ModelState != ModelState)
        {
            db.Projects.Add(project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
    catch (Exception)
    {
        ViewBag.ErrorMessage = "Something went wrong!! Please try again.";
       // Error message
    } 
    ViewBag.ProposerID = new SelectList(db.Proposers, "ID", "ProposerName", project.ProposerID);
    return View(project);
}

和我的观点:

<div class="form-group">
    @Html.LabelFor(model => model.ProjectType, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("ProjectType", "")
        @Html.ValidationMessageFor(model => model.ProjectType, "", new { @class = "text-danger" })
    </div>
</div>

我已经设法显示了数据,但是我对如何获取所选项目并保存它感到困惑。

我的模特是:

public class Project
{
    public int ID { get; set; }
    [Required]
    [DisplayName("Project Title")]
    public string ProjectTitle { get; set; }
    [Required]
    [DataType(DataType.MultilineText)]
    [DisplayName("Project Description")]
    public string Description { get; set; }
    [Required]
    [DataType(DataType.Date)]
    [DisplayName("Proposed Date")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime ProposedDate { get; set; }
    // Development or Research etc.
    [Required]
    [DisplayName("Project Type")]
    public string ProjectType { get; set; }
    // Project Is Taken or Not
    [DisplayName("Project Status")]
    public ProjectStatus ProjectStatus { get; set; }
    [DisplayName("Project Difficulty")]
    public ProjectDifficulty ProjectDifficulty { get; set; }
    [ForeignKey("Proposer")]
    public int ProposerID { get; set; }
    public virtual Proposer Proposer { get; set; }
}

首先更改你的 ViewBag 密钥,使其与模型 属性 名称不同:

ViewBag.ProjectTypeOptions = projectType; 

并且在您看来,这样使用 DropDownListFor 助手:

@Html.DropDownListFor(x=>x.ProjectType, new SelectList(ViewBag.ProjectTypeOptions,"Value","Text"))

现在您将在 ProjectType 属性.

中的模型中发布选定的项目值

你的模型应该有 属性 类型 string 而不是 IEnumerable<SelectListItem> 像这样:

public class Project
{
   public string ProjectType { get;set;}
   ............ 
   ............
}