【发布时间】:2015-11-05 15:56:14
【问题描述】:
我的项目有一个大问题。我有一个具有枚举类型属性的 ViewModel:Constants.Days Day(星期一 = 1 到星期日 = 7),我已经在编辑视图中使用它来生成下拉列表。在其中正确选择了模型的值。我使用 EnumDropDownListFor Helper。
在另一个视图(这次是使用 RenderAction 助手生成的部分视图)中,我尝试做同样的事情,但 DropDown 没有选择模型的日期。第一个选项被选中。我尝试使用 TextBoxFor 并将正确的 Day 放入其中。很奇怪……我试着做了一个经典的 DropDownListFor 但结果是一样的。该值可用,下拉列表正确生成,但模型的 Day 值未在下拉列表中选择。更新也有效。
有人可以帮帮我吗?非常感谢:)
在这里,您可以看到视图的屏幕截图,其中 Day 已正确放入 TextBox 但未在下拉列表中选择。
部分视图:
@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;
}
}
【问题讨论】:
-
显示视图和动作的代码
-
是的,我刚刚编辑过 :)
-
显示计划操作
-
编辑您的问题并仅显示与您的问题相关的代码。我假设您的意思是
Html.RenderAction("UpdateSchedule", lesson);在这种情况下,它不会工作。您不能使用foreach循环为集合项生成表单控件。使用EditorTemplate -
@StephenMuecke 谢谢我会试试的^^我会给出反馈。
标签: c# asp.net asp.net-mvc drop-down-menu