2013-01-06 55 views
0

在我看来,DropDownList显示了正确的字段,但是当我在“编辑或创建”中选择一个字段时,该字段将被保存/修改为NULL。调试时,我可以看到新值不发送。我认为有ID和SurveyID之间的不匹配......无法将选定的项目从DropDownList设置为控制器

查看:

@model Project_ASP_2012.Models.QuestionGroup 

@{ 
ViewBag.Title = "Edit"; 
} 

<h2>Edit</h2> 

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>QuestionGroup</legend> 

    @Html.HiddenFor(model => model.ID) 


    <div class="editor-label"> 
     @Html.LabelFor(model => model.Description) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Description) 
     @Html.ValidationMessageFor(model => model.Description) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.SurveyID, "Survey") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("Id", String.Empty) 
     @Html.ValidationMessageFor(model => model.SurveyID) 
    </div> 

    <p> 
     <input type="submit" value="Save" /> 
    </p> 
</fieldset> 
} 

<div> 
@Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
@Scripts.Render("~/bundles/jqueryval") 
} 

型号:

public class Survey : IEntity 
{ 


    [Key] 
    [Display(Name = "SurveyID")] 
    public int ID { get; set; } 

    [Required(ErrorMessage = "Survey title is required.")] 
    [Display(Name = "Survey Title")] 
    [MaxLength(20, ErrorMessage = "Title cannot be longer than 20 characters.")] 
    public string Title { get; set; } 

    [MaxLength(50, ErrorMessage = "Description cannot be longer than 50 characters.")] 
    public string Description { get; set; } 

    public virtual ICollection<QuestionGroup> QuestionGroups { get; set; } 

} 

public class QuestionGroup : IEntity 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int ID { get; set; } 

    [MaxLength(50, ErrorMessage = "Description cannot be longer than 50 characters.")] 
    public string Description { get; set; } 

    [Display(Name = "SurveyID")] 
    public int? SurveyID { get; set; } 

    public virtual Survey Survey { get; set; } 

} 

控制器:

public ActionResult Edit(int id) 
    { 
     QuestionGroup questiongroup = unitOfWork.QuestionGroupRepository.GetById(id); 
     if (questiongroup == null) 
     { 
      return HttpNotFound(); 
     } 

     PopulateSurveysDropDownList(questiongroup.SurveyID); 
     return View(questiongroup); 
    } 

    // 
    // POST: /QuestionGroup/Edit/5 

    [HttpPost] 
    public ActionResult Edit(QuestionGroup questiongroup) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       unitOfWork.UoWContext.Entry(questiongroup).State = EntityState.Modified; 
       unitOfWork.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
     } 
     catch (DataException) 
     { 
      ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); 
     } 
     PopulateSurveysDropDownList(questiongroup.SurveyID); 
     return View(questiongroup); 
    } 


    private void PopulateSurveysDropDownList(object selectedSurvey = null) 
    { 
     var surveyQuery = unitOfWork.SurveyRepository.Get(
      orderBy: q => q.OrderBy(d => d.Title)); 
     ViewBag.Id = new SelectList(surveyQuery, "Id", "Title", selectedSurvey); 
    } 

回答

0

在你看来,你有下拉为Id而不是SurveyId。另外,你有两次身份证 - 下拉式和隐藏字段。

<div class="editor-field"> 
    @Html.DropDownList("Id", String.Empty) 
    @Html.ValidationMessageFor(model => model.SurveyID) 
</div> 
0

尝试改变:

private void PopulateSurveysDropDownList(object selectedSurvey = null) 
{ 
    var surveyQuery = unitOfWork.SurveyRepository.Get(
     orderBy: q => q.OrderBy(d => d.Title)); 
    ViewBag.Id = new SelectList(surveyQuery, "Id", "Title", selectedSurvey); 
} 

.... 

<div class="editor-field"> 
    @Html.DropDownList("Id", String.Empty) 
    @Html.ValidationMessageFor(model => model.SurveyID) 
</div> 

要:

private void PopulateSurveysDropDownList(object selectedSurvey = null) 
{ 
    var surveyQuery = unitOfWork.SurveyRepository.Get(
     orderBy: q => q.OrderBy(d => d.Title)); 
    //don't provide the select value here. you will bind to it in your view 
    ViewBag.SurveySelectList = new SelectList(surveyQuery, "Id", "Title"); 
} 

.... 

<div class="editor-field"> 
    //selected value will be whatever SurveyID is on your model. 
    @Html.DropDownListFor(model => model.SurveyID, ViewBag.SurveySelectList, String.Empty) 
    @Html.ValidationMessageFor(model => model.SurveyID) 
</div> 

希望有所帮助。

相关问题