2013-01-06 2 views
0

으로 설정할 수 없습니다. 내보기에서 DropDownList는 올바른 필드를 표시하지만 "편집 또는 만들기"에서 필드를 선택하면 NULL로 저장/수정됩니다. 디버깅 할 때 새로운 값이 전송되지 않음을 알 수 있습니다.선택한 항목을 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 사이에 불일치가 있다고 생각 Id 대신에 SurveyId가 필요합니다. 드롭 다운과 숨겨진 필드에서 ID가 두 번 나타납니다.

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

희망.

관련 문제