MVC 4 Changing a field based on DropDownListFor selection
首先,几乎与上述问题相同,但解决方案不适用于此问题。
我有一个包含下拉列表的页面。选择后,它将根据选择更改显示字段。
视图中的javascript是:
eslint
观点:
<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>
Controller方法:
<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>
目前javascript没有被点击,或者我的方法没有被调用。
答案 0 :(得分:2)
您正在做的一件事是不从您的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小提琴。 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();
答案 1 :(得分:0)
使用以下功能更改您的Javascript功能:
$("#CourseName").change(function () {
var courseID = $(this).val();
$.ajax({
type: 'POST',
url: '@Url.Action("FillCourseInfo","Student")', // we are calling json method
dataType: 'json',
data: { StudentID: @ViewBag.studentID, CourseID, courseID },
success: function (ret) {
$('#Dates').val(ret.courseDates);
$('#Project').val(ret.projectName);
$('#Graduated').val(ret.graduated);
},
error: function (ex) {
//alert('Failed to retrieve states.' + ex);
}
});
return false;
});
使用以下功能
更改控制器功能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 StudentCourseDetails
{
courseDates = c.CourseStartDate.ToString() + " " + c.CourseEndDate.ToString(),
projectName = e.Project.ProjectTitle,
graduated = e.Graduated
});
return Json(ret);
}