0

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" }))
smuldr
  • 315
  • 1
  • 12
  • 2
    and what's the error? – Ehsan Sajjad Dec 17 '18 at 18:54
  • 1
    is the action method decorated with the `HttpPost` attribute ? – Ehsan Sajjad Dec 17 '18 at 18:56
  • The error is a 404 error and yes the method has the HttpPost attribute – smuldr Dec 17 '18 at 19:03
  • Regardless of the 404 I'd expect you'd need to put `e.preventDefault();` right at the start of your function. You can't wait until the AJAX call returns - bearing in mind that AJAX is async, by the time that runs, your form will probably already have submitted itself. – ADyson Dec 17 '18 at 19:05
  • I tried putting the e.preventDefault(); at the beginning to stop the form post but I'm still getting the ajax error. – smuldr Dec 17 '18 at 19:08
  • 1
    Generally you don’t include ’controller’ when specifying ’RequestController’. – Mackan Dec 17 '18 at 19:14
  • @Mackan you got sharp eyes, that is the problem i believe – Ehsan Sajjad Dec 17 '18 at 19:16
  • @suprkain I did say "Regardless of the 404"...as in, I don't think this will fix your issue. But it might well cause you a problem at other times, e.g. if the ajax is slower than usual to respond. That was my point. You need to _always_ prevent the default submit, in order to allow time for the check to take place. And then if it turns out everything's ok, then explicitly call .submit() on your form. – ADyson Dec 17 '18 at 19:53
  • @suprkain But actually then again...why do any of this? You're calling a server-side method to validate form which you're about to submit to the server. Why not just do all the validation in the main submit, and thereby save yourself a HTTP request, and quite a bit of code? The business logic to do it is already on the server, after all. This seems like a bit of an odd layer of extra guff to achieve what could be done much more simply. – ADyson Dec 17 '18 at 19:54
  • @suprkain And also, since someone could just turn off all this JS in the browser and stop the AJAX from ever happening...then you have to do that validation in the main submit anyhow, otherwise you haven't got any guarantee of valid data at all. So it all seems a little pointless, unless I've missed something? – ADyson Dec 17 '18 at 19:55
  • @ADyson - Well I do have validation I wrote in the submit form method however if the modelstate is invalid due to an error I added to the model I am getting an 'Index' or its master was not found error. I've tried all the suggestions and even submitted another post located here: https://stackoverflow.com/questions/53817340/mvc-index-or-its-master-was-not-found However I have not had any luck solving this problem. – smuldr Dec 17 '18 at 19:56
  • @suprkain a bit of an error in your main validation process would just need fixing, rather than this elaborate workaround, surely? Model validation in MVC is fairly simple. And anyway as I said, if you don't get the validation working on the main submit, you effectively have no guaranteed validation at all. Plus once you actually come to submit the form you'll just end up with the same problem, I would think – ADyson Dec 17 '18 at 20:00
  • @AdDyson - I know I'm just trying a different route. If you wouldn't mind could you click that link and see if I'm missing something there. – smuldr Dec 17 '18 at 20:01

1 Answers1

1

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")',
Mackan
  • 6,200
  • 2
  • 25
  • 45