我的项目存在很大问题。我有一个具有Enum类型属性的ViewModel:Constants.Days Day(星期一= 1到星期日= 7)我已经在编辑视图中使用它来生成下拉列表。在其中正确选择了模型的值。我使用EnumDropDownListFor Helper。
在另一个视图中(这次使用RenderAction助手生成的局部视图)我尝试做同样的事情,但DropDown没有选择模型的日子。第一个选项被选中。我尝试使用TextBoxFor并将正确的日期放入其中。这很奇怪......我试着做一个经典的DropDownListFor但结果是一样的。该值可用,下拉列表生成正确,但未在DropDown中选择模型的日期值。更新也有效。
是的,有人可以帮帮我吗?非常感谢你:))在这里,您可以看到View的屏幕截图,其中Day正确放入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">×</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;
}
}
答案 0 :(得分:0)
在您的计划GET操作中,尝试将vm.Day
设置为您希望下拉菜单选择的任何值。