У меня проблема с переменной, принимающей значение undefined
.Почему переменная URL не определена?
В файле Create.cshtml я есть:
@using (Html.BeginForm("Create", "Enrollment", FormMethod.Post, new
{
id = "YearCourseFormId",
data_courseListAction = @Url.Action("CourseList")
})) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Enrollment</legend>
<div class="editor-label">
@Html.LabelFor(model => model.StudentId, "Student")
</div>
<div class="editor-field">
@Html.DropDownList("StudentId", ViewBag.Student as SelectList, String.Empty, new { @class = "chosen-select", data_placeholder = "Please Select a Student...",style="width:350px;" })
@Html.ValidationMessageFor(model => model.StudentId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Course.YearId, "Year")
</div>
<div class="editor-field">
@Html.DropDownList("YearId", ViewBag.Year as SelectList, String.Empty, new { @class = "chosen-select", data_placeholder = "Please Select a Year...",style="width:250px;" })
@Html.ValidationMessageFor(model => model.Course.YearId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CourseId, "Course")
</div>
<div class="editor-field">
@Html.DropDownList("CourseId", String.Empty)
@Html.ValidationMessageFor(model => model.CourseId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
В файле Jquery я есть:
$(function() {
$('#YearId').change(function() {
var URL = $('#YearCourseFormId').data('courseListAction');
alert(URL);
$.getJSON(URL + '/' + $('#YearId').val(), function (data) {
var selectList = $("#CourseId");
selectList.empty();
var option = $('<option>');
selectList.append(option);
$.each(data, function (index, optionData) {
option = $('<option>').text(optionData.Text).val(optionData.Value);
selectList.append(option);
});
});
});
});
В этой части alert(URL);
результат не определен.
В EnrollmentController.cs я есть:
public ActionResult CourseList(string ID)
{
int Year = int.Parse(ID);
var courses = from s in db.Courses
where s.YearId == Year
select s;
if (HttpContext.Request.IsAjaxRequest())
return Json(new SelectList(
courses.ToArray(),
"Id",
"CourseName")
, JsonRequestBehavior.AllowGet);
return RedirectToAction("Index");
}
Но этот метод не вызывается из-за ошибки.
В чем проблема?
Нам нужно будет увидеть html '# YearCourseFormId', я подозреваю, что вы неправильно создали атрибут dataAttribute как' data-courseListAction = "somevalue", а не 'data-course-list-action =" somevalue "' –
Этот код серьезно неполна. Что такое 'data_courseListAction'? Что ты делаешь с этим? Как выглядит визуализированный HTML, отправленный в браузер? –
@KevinB: Ах, да, еще одна жертва ненужной магии данных «jQuery» ... Я забыл эту определенную морщину. –