Search code examples
ajaxasp.net-mvchtml.beginform

Ajax request off an MVC Form


I am trying to do an ajax request and depending on the ajax request results I will allow the form to submit to the controller. However everytime the ajax request runs I get the error message.

Here is my javascript function:

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

This function is called from here:

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

I try to debug the code and put a breakpoint on Validate_ScholarshipRequest but that method never gets called. The method signature is:

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

This is the start of my form:

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

Solution

  • Just to get this officially answered and "closed", this was caused by a syntax-error:

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

    Controller should not be included in the controller name. The correct action would then be:

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