我有以下问题无法从下拉列表中获取我尝试了几周来解决的值。以下是我正在使用的视图模型
namespace Training.ViewModels
{
public class RosterViewModel
{
public Guid RosterViewModelId { get; set; }
public List<ClassAttendance> ClassAttendances { get; set; }
public TRSClass TRSClass { get; set; }
public ClassRoster Roster { get; set; }
public Student Student { get; set; }
public string AttendanceCodeId { get; set; }
}
public class ClassAttendance
{
public List<Guid> ClassRosterId { get; set; }
public int EnrollmentId {get; set;}
public string StudentName { get; set; }
public List<int> AttendanceCodeId { get; set; }
public List<DateTime> ClassDate { get; set; }
public IEnumerable<SelectListItem> AttendnaceCodes { get; set; }
}
}
使用的视图具有以下代码
@model Training.ViewModels.RosterViewModel
@using Training.UtilModels
@{
ViewBag.Title = "Class Attendance";
}
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.RosterViewModelId)
<table id="Events" class="table table-striped">
<thead>
<tr>
<th></th>
<th>Student</th>
@for (int i = 1; i <= Model.TRSClass.Course.NoDays; i++)
{
<th>@BusinessDay.AddWorkDays(Model.TRSClass.ClassStartDate, i).AddDays(-1).ToShortDateString()</th>
}
</tr>
</thead>
<tbody>
@* @foreach (var x in Model.ClassAttendances)*@
@for (int i = 0; i < Model.ClassAttendances.Count; i++ )
{
<tr>
<td>
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].EnrollmentId)
</td>
<td>@Html.DisplayFor(modelItem => modelItem.ClassAttendances[i].StudentName)</td>
@for (int y = 0; y < Model.TRSClass.Course.NoDays; y++)
{
<td>
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].ClassRosterId[y])
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].AttendanceCodeId[y], new { @id = "codeId" })
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].ClassDate[y])
@Html.DropDownList("AttendanceCodeId", ViewBag.AttendanceCodeId as SelectList, Model.ClassAttendances[i].AttendanceCodeId[y] != 0 ? Model.ClassAttendances[i].AttendnaceCodes.Where(t => t.Value == Model.ClassAttendances[i].AttendanceCodeId[y].ToString()).Select(t => t.Text).FirstOrDefault() : "Select", new { @id = "codelist" })
</td>
}
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@if (Model.TRSClass.ClassEndDate >= DateTime.Today)
{
<div class="btn btn-default">
<input type="submit" value="Save" class="btn btn-default" />
</div>
}
<div class="btn btn-default">
@Html.ActionLink("Class List", "Index", "Classes")
</div>
</div>
</div>
}
@section scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready(function () {
$('#codelist').change(function () {
var res = $(this).val();
$('#codeId').val(res);
});
});
</script>
}
和控制器编辑功能如下:
// GET: Class Roster
public ActionResult Edit(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
RosterViewModel model = new RosterViewModel();
model.RosterViewModelId = (Guid)id;
model.ClassAttendances = new List<ClassAttendance>();
var roster = db.ClassRoster.Where(c => c.TRSClass.TRSClassId == id)
.Include(c => c.TRSClass)
.Include(c => c.Enrollments)
.OrderBy(c => c.EnrollmentId).ThenBy(c => c.ClassDate)
.ToList();
model.TRSClass = roster.Select(c => c.TRSClass).FirstOrDefault();
ViewBag.AttendanceCodeId = db.AttendanceCodes.Select(m => new SelectListItem { Text = m.AttendanceCd, Value = m.AttendanceCodeId.ToString() });
ClassAttendance a = new ClassAttendance();
a.ClassRosterId = new List<Guid>();
a.AttendanceCodeId = new List<int>();
a.ClassDate = new List<DateTime>();
a.AttendnaceCodes = db.AttendanceCodes.Select(m => new SelectListItem { Text = m.AttendanceCd, Value = m.AttendanceCodeId.ToString() });
string studentName = model.TRSClass.Enrollments.OrderBy(e => e.EnrollmentId).Select(e => e.Student.FullName).FirstOrDefault();
int i = 1;
foreach (ClassRoster c in roster)
{
if (!studentName.Equals(c.TRSClass.Enrollments.Where(t => t.EnrollmentId == c.EnrollmentId).Select(t => t.Student.FullName).FirstOrDefault()))
{
model.ClassAttendances.Add(a);
a = new ClassAttendance();
a.ClassRosterId = new List<Guid>();
a.AttendanceCodeId = new List<int>();
a.ClassDate = new List<DateTime>();
a.AttendnaceCodes = db.AttendanceCodes.Select(m => new SelectListItem { Text = m.AttendanceCd, Value = m.AttendanceCodeId.ToString() });
studentName = c.TRSClass.Enrollments.Where(t => t.EnrollmentId == c.EnrollmentId).Select(t => t.Student.FullName).FirstOrDefault();
i = 1;
}
a.ClassRosterId.Add((Guid)c.ClassRosterId);
a.EnrollmentId = c.EnrollmentId;
a.StudentName = c.TRSClass.Enrollments.Where(t => t.EnrollmentId == c.EnrollmentId).Select(t => t.Student.FullName).FirstOrDefault();
a.AttendanceCodeId.Add(c.AttendanceCodeId);
a.ClassDate.Add(BusinessDay.AddWorkDays(model.TRSClass.ClassStartDate, i).AddDays(-1));
i++;
}
model.ClassAttendances.Add(a);
return View(model);
}
[HttpPost]
public ActionResult Edit(RosterViewModel model)
{
if (ModelState.IsValid)
{
TRSClass theClass = db.Classes.Find(model.RosterViewModelId);
//db.Entry(model).State = EntityState.Modified;
foreach (ClassAttendance c in model.ClassAttendances)
{
for (int i = 0; i < theClass.Course.NoDays; i++)
{
ClassRoster CR = db.ClassRoster.Find(c.ClassRosterId[i]);
CR.AttendanceCodeId = c.AttendanceCodeId[i];
}
}
db.SaveChanges();
return RedirectToAction("Index", new { id = theClass.TRSClassId });
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
}
我似乎无法从下拉列表中获取值或获取jquery函数来更新隐藏字段。任何见解都将受到赞赏。
答案 0 :(得分:0)
在我看来,我更改了循环以包含下拉列表的索引和下面的相关字段代码
@{
int z = 0;
for (int i = 0; i < Model.ClassAttendances.Count; i++ )
{
<tr>
<td>
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].EnrollmentId)
</td>
<td>@Html.DisplayFor(modelItem => modelItem.ClassAttendances[i].StudentName)</td>
@for (int y = 0; y < Model.TRSClass.Course.NoDays; y++)
{
<td>
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].ClassRosterId[y])
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].AttendanceCodeId[y], new { @id = "codeId" + @z })
@Html.HiddenFor(modelItem => modelItem.ClassAttendances[i].ClassDate[y])
@Html.DropDownList("AttendanceCodeId", ViewBag.AttendanceCodeId as SelectList, Model.ClassAttendances[i].AttendanceCodeId[y] != 0 ? Model.ClassAttendances[i].AttendnaceCodes.Where(t => t.Value == Model.ClassAttendances[i].AttendanceCodeId[y].ToString()).Select(t => t.Text).FirstOrDefault() : "Select", new { @id = "codelist" + @z, @class = "form-control" })
</td>
z++;
}
</tr>
}
}
然后我修改了我的JQuery脚本以循环操作并使用新值更新模型,然后将其发布到控制器以进行数据库更新
<script type="text/javascript">
$(document).ready(function () {
$('.form-control').each(function(i){
$('#codelist' + i).change(function () {
var res = $(this).val();
$('#codeId' + i).val(res);
});
});
});
</script>