MVC 4 Changing a field based on DropDownListFor selection
首先,与上述问题几乎相同,但解决方案不适用于此问题。
我有一个包含下拉列表的页面。选择后,它将根据选择更改显示字段。
View 中的 javascript 是:
<script type="text/javascript">
$(function(){
$('#courseName').on('change', function () {
var courseID = $(this).val();
var studentID = '@ViewBag.StudentID';
$.ajax({
url: '@Url.Action("FillCourseInfo", "Student")',
data: {StudentID: studentID, CourseID: courseID},
type: 'get'
}).done(function(data){
$('#courseStartDate').text(data.courseStartDate);
$('#courseEndDate').text(data.courseEndDate);
$('#projectName').text(data.projectName);
$('#graduated').text(data.graduated);
});
});
});
</script>
View :
<tr>
<th class="table-row">
Course Title:
</th>
<td class="table-row">
@Html.DropDownListFor(m => m.courseName,
@ViewBag.courseList as SelectList, " -- Select Course -- ",
new { @class = "form-control" })
</td>
</tr>
<tr>
<th class="table-row">
Start & End Date:
</th>
<td class="table-row">
<label id="#courseStartDate" class="form-control">@Model.courseStartDate</label>
</td>
<td class="table-row">
<label id="#courseEndDate" class="form-control">@Model.courseEndDate</label>
</td>
</tr>
<tr>
<th class="table-row">
Project:
</th>
<td class="table-row">
<label id="#projectName" class="form-control">@Model.projectName</label>
</td>
</tr>
<tr>
<th class="table-row">
Graduated:
</th>
<td class="table-row">
<label id="#graduated">@Model.graduated</label>
</td>
</tr>
和 Controller 方法:
[HttpGet]
public JsonResult FillCourseInfo(int StudentID, int CourseID)
{
var ret = (from e in db.Enrollments
join c in db.Courses on e.CourseID equals c.CourseID
where e.StudentID == StudentID && e.CourseID == CourseID
select new
{
courseStartDate = c.CourseStartDate,
courseEndDate = c.CourseEndDate,
projectName = e.Project.ProjectTitle,
graduated = e.Graduated
}).ToList()
.Select(a => new StudentCourseDetails() {
courseStartDate = a.courseStartDate.ToString("MMM d, yyyy"),
courseEndDate = a.courseEndDate.ToString("MMM d, yyyy"),
projectName = a.projectName,
graduated = a.graduated.Value.ToString()
}).FirstOrDefault();
string sd = ret.courseStartDate;
string ed = ret.courseEndDate;
string pn = ret.projectName;
string g = ret.graduated;
return Json(ret, JsonRequestBehavior.AllowGet);
}
目前 javascript 没有被调用或者我的方法没有被调用。
最佳答案
您正在做的一件事是不从您的 FillCourseInfo
操作返回单个结果。
这意味着您的 json 结果是 StudentCourseDetails
列表。您必须使用 $('#Dates').val(data[0].courseDates);
来获取您的值。
或者,如果您只需要一个值,您可以在 linq 查询末尾使用 .FirstOrDefault()。
var ret = (from e in db.Enrollments
join c in db.Courses on e.CourseID equals c.CourseID
where e.StudentID == StudentID && e.CourseID == CourseID
select new StudentCourseDetails
{
courseDates = c.CourseStartDate.ToString() + " " + c.CourseEndDate.ToString(),
projectName = e.Project.ProjectTitle,
graduated = e.Graduated
}).FirstOrDefault();
我为您创建了另一个 .NET Fiddle。 DotNetFiddle
要在您的 linq 查询中使用 ToString,请将结果转换为列表,然后构建您的 json
var ret = (from e in db.Enrollments
join c in db.Courses on e.CourseID equals c.CourseID
where e.StudentID == StudentID && e.CourseID == CourseID
select new
{
courseStartDate = c.CourseStartDate,
courseEndDate = c.CourseEndDate,
projectName = e.Project.ProjectTitle,
graduated = e.Graduated
}).ToList()
.Select(a => new StudentCourseDetails() {
courseDates = a.courseStartDate.ToString() + " " + a.courseEndDate.ToString(),
projectName = a.projectName,
graduated = a.graduated
}).FirstOrDefault();
关于javascript - MVC 4 根据 DropDownListFor 选择更改多个显示字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31192524/