我正在使用EnumDropDownListFor
,但它没有将值传输到Binding模型。 CourseId
和StudentId
到达绑定模型,但不到达枚举值。
感谢您的帮助!!
这是我的观点:
@model IEnumerable<LeisureTimeSystem.Models.ViewModels.Course.CourseApplicationViewModel>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Student.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Student.Username)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Student.Email)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
using (Html.BeginForm())
{
@Html.Hidden("CourseId", item.Course.Id)
@Html.Hidden("StudentId", item.Student.Id)
<tr>
<td>
@Html.DisplayFor(model => item.Student.Name)
</td>
<td>
@Html.DisplayFor(model => item.Student.Username)
</td>
<td>
@Html.DisplayFor(model => item.Student.Email)
<td>
@Html.DisplayFor(model => item.Status)
</td>
@Html.EnumDropDownListFor(model => item.Status, "Change Status", new { @class = "form-control" })
<div class="form-group">
<input type="submit">
</div>
</tr>
}
}
</table>
这是我的观点模型:
public class CourseApplicationViewModel
{
public ApplicationCourseViewModel Course { get; set; }
public ApplicationStudentViewModel Student { get; set; }
public ApplicationStatus Status { get; set; }
}
这是我的Binding模型:
public class ChangeStatusApplicationBindingModel
{
public int StudentId { get; set; }
public int CourseId { get; set; }
public ApplicationStatus Status { get; set; }
}
这是Controller的行动:
[HttpPost]
public ActionResult ManageCourseApplications(ChangeStatusApplicationBindingModel model)
{
if (this.ModelState.IsValid)
{
this.service.ChangeStatus(model);
return RedirectToAction("ManageCourseApplications", new { studentId = model.StudentId, courseId = model.CourseId });
}
var changeStatusViewModel = this.service.GetAllCourseApplicationViewModels(model.CourseId);
return this.PartialView(changeStatusViewModel);
}
枚举:
public enum ApplicationStatus
{
AwaitingConfirmation,
Approved,
Denied
}
HTML:
<div class="container body-content">
<h2>Shakespeare Applications</h2>
<p>
<a href="/Course/Create">Create New</a>
</p>
<select class="form-control" data-val="true" data-val-required="The Status field is required." id="item_Status" name="item.Status"><option value="">Change Status</option>
<option selected="selected" value="0">AwaitingConfirmation</option>
<option value="1">Approved</option>
<option value="2">Denied</option>
</select><div class="form-group">
<input type="submit">
</div><table class="table">
<tbody><tr>
<th>
Name
</th>
<th>
Username
</th>
<th>
Email
</th>
<th></th>
</tr>
<form action="/Course/ManageCourseApplications?courseId=4" method="post"></form><input id="CourseId" name="CourseId" type="hidden" value="4"><input id="StudentId" name="StudentId" type="hidden" value="1"> <tr>
<td>
Secondsdasa
</td>
<td>
TheBest
</td>
<td>
<a href="mailto:"></a>
</td><td>
AwaitingConfirmation
</td>
</tr>
</tbody></table>
<hr>
<footer>
<p>© 2017 - My ASP.NET Application</p>
</footer>
</div>
&#13;