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

时间:2015-05-18 15:00:35

标签: c# asp.net-mvc selectlistitem

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

1 个答案:

答案 0 :(得分:0)

首先更改您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;}
   ............ 
   ............
}
相关问题