0

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

1 回答 1

0

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

ViewBag.ProjectTypeOptions = projectType; 

并在您看来以DropDownListFor这种方式使用助手:

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

ProjectType现在您将获得在属性中发布在模型中的选定项目值。

并且您的模型应该具有stringIEnumerable<SelectListItem>这样的类型的属性:

public class Project
{
   public string ProjectType { get;set;}
   ............ 
   ............
}
于 2015-05-18T15:30:20.310 回答