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

本文关键字:数据 下拉列表 selectlist 保存 使用 | 更新日期: 2023-09-27 17:49:31

我正在尝试使用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);
}

and my View:

<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>

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

My Model is:

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

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

首先更改ViewBag键,使其与模型属性名称不同:

ViewBag.ProjectTypeOptions = projectType; 

和在您的视图中使用DropDownListFor helper如下:

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

现在你会得到选定的项目值张贴在模型在ProjectType属性。

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

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