为什么变量URL未定义?

时间:2013-08-21 21:25:41

标签: c# javascript jquery asp.net-mvc

我遇到一个带有值undefined的变量的问题。

在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");
    }

但是由于错误而没有调用此方法。

有什么问题?

1 个答案:

答案 0 :(得分:4)

您的表单呈现如下:

<form action="/Enrollment/Create" data-courselistaction="..." id="YearCourseFormId" method="post">

</form>

您可以使用以下任何一种方式进行访问:

$('#YearCourseFormId').data('courselistaction');
$('#YearCourseFormId').attr('data-courselistaction');
$('#YearCourseFormId').attr('data-courseListAction');

鉴于T.J.克劳德关于data()的怪癖的评论,与其中一个attr()解决方案一起使用可能会更安全。

编辑 - 情节变浓..

Chrome inspecter:

inspecter

来源:

source

我不确定其含义是什么,但是我的javascript代码段确实能够获得价值。