2015-11-05 28 views
2

我的項目有一個大問題。我有一個具有Enum類型屬性的ViewModel:Constants.Days日(星期一= 1到星期日= 7),我已經在編輯視圖中使用它來生成下拉列表。模型的值在其中被正確選擇。我使用EnumDropDownListFor Helper。ASP.NET EnumDropDownListFor生成DropDown但不選擇模型的值

在其他視圖(這次用RenderAction助手生成的部分視圖)我嘗試做同樣的事情,但DropDown不選擇模型的日子。第一個選項被選中。我嘗試使用TextBoxFor,並將正確的Day放入其中。這很奇怪......我試圖做一個經典的DropDownListFor但結果是一樣的。該值可用,下拉菜單正確生成,但模型的Day值未在DropDown中選擇。更新也可以工作。

有人可以幫我嗎?非常感謝:)

在這裏你可以看到一個視圖的截圖,其中一天正確放置在一個文本框中,但未在下拉列表中選擇。

Screenshot of the View with Dropdown and textbox

的PartialView:

@model p3t.Models.ScheduledLessonViewModel 

<div class="modal" id="@("updateLessonModal" + Model.ScheduledLessonId)" tabindex="-1" role="dialog" aria-labelledby="updateLessonModalLabel"> 
    <div class="modal-dialog modal-lg"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <h4 class="modal-title" id="myModalLabel">Planifier un cours</h4> 
     </div> 
     @using (Html.BeginForm("UpdateSchedule", "Home")) 
     { 
      @Html.AntiForgeryToken() 

      <div class="form-horizontal "> 
       <div class="modal-body"> 
        @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

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

        <div class="form-group"> 
         @Html.LabelFor(model => model.Day, htmlAttributes: new { @class = "control-label col-md-2" }) 
         <div class="col-md-10"> 
          @Html.TextBoxFor(model => model.Day, htmlAttributes: new { @class = "form-control btn btn-default col-md-10", disabled = "disabled" }) 
         </div> 
        </div> 
       </div> 
      </div> 

      <div class="modal-footer"> 
       <button type="submit" class="btn btn-primary">Mettre à jour</button> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button> 
      </div> 
     } 
    </div> 
</div> 

@model p3t.Models.ScheduleViewModel 
@using p3t.Models.Dal.Dto 


@foreach (var lesson in Model.ScheduledLessons) 
{ 
    if (lesson.Section.SectionId == Model.Section.SectionId) 
    { 
     if (day.Equals(lesson.Day.ToString()) && lesson.BeginHour.Hours == h) 
     { 
      Html.RenderAction("UpdateSchedule", lesson); 

      <div class="lesson"> 

      @if (User.IsInRole("Administrateur") || User.IsInRole("Secrétaire")) 
      { 
      <button id="@lesson.ScheduledLessonId" class="pull-left glyphicon glyphicon-cog" data-toggle="modal" data-target="#@("updateLessonModal" + lesson.ScheduledLessonId)"></button> 
      <button id="" class="pull-left glyphicon glyphicon-trash"</button> 
      } 
     } 
    } 
} 

個控制器的操作爲查看:

public ActionResult UpdateSchedule(ScheduledLessonModel model) 
     { 
      ScheduledLessonViewModel slvm = new ScheduledLessonViewModel(); 
      slvm.LessonList = new List<SelectListItem>(); 
      slvm.SectionList = new List<SelectListItem>(); 
      slvm.ClassroomList = new List<SelectListItem>(); 

      var lessonList = lessonRepository.GetAll(); 
      var sectionList = sectionRepository.GetAll(); 
      var classroomList = classroomRepository.GetAll(); 

      foreach (var item in lessonList) 
      { slvm.LessonList.Add(new SelectListItem { Value = item.LessonId.ToString(), Text = item.Name }); } 
      foreach (var item in sectionList) 
      { slvm.SectionList.Add(new SelectListItem { Value = item.SectionId.ToString(), Text = item.Tag }); } 
      foreach (var item in classroomList) 
      { slvm.ClassroomList.Add(new SelectListItem { Value = item.ClassroomId.ToString(), Text = item.Name }); } 

      slvm.ScheduledLessonId = model.ScheduledLessonId; 
      slvm.Day = model.Day; 
      slvm.BeginHour = model.BeginHour; 
      slvm.EndHour = model.EndHour; 
      slvm.Quadrimester = model.Quadrimester; 
      slvm.LessonId = model.Lesson.LessonId; 
      slvm.SectionId = model.Section.SectionId; 
      slvm.ClassroomId = model.Classroom.ClassroomId; 

      return PartialView("ScheduleModal", slvm); 
     } 

     [HttpPost] 
     public ActionResult UpdateSchedule(ScheduledLessonViewModel slvm) 
     { 
      try 
      { 
       ScheduledLessonModel slmodel = new ScheduledLessonModel(); 
       slmodel.Lesson = new LessonModel(); 
       slmodel.Section = new SectionModel(); 
       slmodel.Classroom = new ClassroomModel(); 

       slmodel.ScheduledLessonId = slvm.ScheduledLessonId; 
       slmodel.Day = slvm.Day; 
       slmodel.BeginHour = slvm.BeginHour; 
       slmodel.EndHour = slvm.EndHour; 
       slmodel.Quadrimester = slvm.Quadrimester; 
       slmodel.Lesson.LessonId = slvm.LessonId; 
       slmodel.Section.SectionId = slvm.SectionId; 
       slmodel.Classroom.ClassroomId = slvm.ClassroomId; 

       scheduledLessonRepository.Update(slmodel); 

       return RedirectToAction("Schedule", new { sectionId = slvm.SectionId }); 
      } 
      catch (Exception) 
      { 

       throw; 
      } 
     } 
+2

表明的觀點和行動 –

+0

是啊,我剛纔編輯:) –

+0

顯示附表行動 – JB06

回答

0

在你的日程表Get行動,嘗試設置vm.Day任何值你想你的下拉來選擇。

+0

我有2個Modals:一個用於創建,一個用於編輯。問題出在編輯模式中,未選擇模型的日期值:) –

+0

好的,抱歉誤會。 model.Day在創建EnumDropDownFor時的視圖中有什麼價值? – JB06