gpt4 book ai didi

c# - ASP.NET EnumDropDownListFor 生成 DropDown 但不选择模型的值

转载 作者:太空宇宙 更新时间:2023-11-03 15:31:45 25 4
gpt4 key购买 nike

我的项目有一个大问题。我有一个具有枚举类型属性的 ViewModel:Constants.Days Day(星期一 = 1 到星期日 = 7),我已经在编辑 View 中使用它来生成下拉列表。在其中正确选择了模型的值。我使用 EnumDropDownListFor Helper。

在另一个 View (这次是使用 RenderAction 助手生成的部分 View )中,我尝试做同样的事情,但 DropDown 不选择模型的日期。选择第一个选项。我尝试使用 TextBoxFor 并将正确的 Day 放入其中。这很奇怪......我尝试做一个经典的 DropDownListFor 但结果是一样的。该值可用,下拉列表已正确生成,但未在下拉列表中选择模型的 Day 值。更新也有效。

有人可以帮我吗?非常感谢:)

在这里您可以看到 View 的屏幕截图,其中日期已正确放入文本框但未在下拉列表中选中。

Screenshot of the View with Dropdown and textbox

局部 View :

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

View

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

Controller 对 View 的操作:

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

最佳答案

在您的 Schedule GET 操作中,尝试将 vm.Day 设置为您希望下拉列表选择的任何值。

关于c# - ASP.NET EnumDropDownListFor 生成 DropDown 但不选择模型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com