I successfully executed this action in a .js file without any complications, but now I'm facing difficulties implementing it in a .cshtml file. Despite thorough investigation, I can't identify any other potential reasons for this failure. Below is the JavaScript code written within my .cshtml file:
mergeBtn.onclick = function (e) {
e.preventDefault();
var url = '/api/publicpatron/student-no-validation?studentNo=' + studentNo.value;
$.getJSON(url)
.done(function (json) {
if (json.errors) {
toastr.error(json.message, '', { timeOut: 0, extendedTimeOut: 0 })
}
else {
//do something
}
})
.fail(function (jqxhr, textStatus, error) {
var err = textStatus = ', ' + error;
toastr.error(err, '', { timeOut: 0, extendedTimeOut: 0 })
})
}
The issue doesn't seem to lie in the controller code as it never reaches the controller itself. I've double-checked the file and function names in the URL, so that shouldn't be causing the problem. Any suggestions on what could be going wrong? Is executing this from a .cshtml file not feasible???
UPDATE:
Below is the information about the controller:
File name: PublicPatronController
[Authorize(Roles = "my-roles")]
[ActionName("student-no-validation")]
public dynamic IsStudentNoValid([FromUri] string studentNo)
{
dynamic results = new ExpandoObject();
if (studentNo == null)
{
results.error = true;
results.message = "Invalid Student Number";
return results;
}
using (ADRoutineEntities db = new ADRoutineEntities())
{
var exists = db.UserLinkages.Any(x => x.StudentNo == studentNo);
if (!exists)
{
results.errors = true;
results.message = string.Format("Student number {0} does not exist", studentNo);
return results;
}
}
results.ok = true;
return results;
}
UPDATE 2:
It seems like the issue might be related to the controller after all. I switched the URL to a different apicontroller
used elsewhere, and it worked perfectly. It appears that the problem lies with the name of the apicontroller
. When I switch it to an existing apicontroller
while keeping the actionname
the same, everything works fine. Why do you think this is happening???