Ajax请求MVC表单

时间:2018-12-17 18:51:34

标签: ajax asp.net-mvc html.beginform

我正在尝试执行ajax请求,并且根据ajax请求的结果,我将允许表单提交给控制器。但是,每次ajax请求运行时,我都会收到错误消息。

这是我的JavaScript函数:

function CheckForValidation(e) {
    var scholarshipRequest = $("#scholars").val();
    var aidYearRequest = $("#aidYear").val();
    var amountRequest = $("#amount").val();

    $.ajax({
        type: "POST",
        url: '@Url.Action("Validate_ScholarshipRequest", "RequestController")',
        data: {
            scholarshipId: scholarshipRequest,
            aidYear: aidYearRequest, 
            amount: amountRequest
        }
    }).success(function(response) {
        if (!response.success) {
            e.preventDefault();
            alert(success);
        } else {
            e.preventDefault();
        }
    }).error(function() {
        e.preventDefault();
        alert("Error on Submission");
    });
}

此函数从这里调用:

$("#SubmitTutorRequestFrm").submit(function(e) {
    e.PreventDefault();
    CheckForValidation(e);
});

我尝试调试代码,并在Validate_ScholarshipRequest上放置一个断点,但从未调用该方法。方法签名为:

public ActionResult Validate_ScholarshipRequest(string scholarshipId, string aidYear, string amount)

这是我表格的开始

@using (Html.BeginForm("SubmitScholarshipRequest", "Request", FormMethod.Post, new { id = "SubmitTutorRequestFrm" }))

1 个答案:

答案 0 :(得分:1)

仅是为了得到正式答复并“关闭”,这是由语法错误引起的:

url: '@Url.Action("Validate_ScholarshipRequest", "RequestController")',

Controller 不应包含在控制器名称中。正确的操作将是:

url: '@Url.Action("Validate_ScholarshipRequest", "Request")',