I got the simple usecase to just send an AJAX request to my controller and receive the result. My current Problem is that the Ajax call returns success with the complete html code of the current page as content and not the return value of my Controller method.
I am a web developer beginner, hints regarding the IIS Express configuration in Visual Studio might also help. Even if this peace of code is part of a larger project.
I know there are a lot of similar questions, but they didn't help at all: javascript ajax call not not hitting controller Asp.net MVC Ajax call not calling controller method
This is my Razor Code:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.Partial("CEinzelkondition", Model)
<input id="SubmitButton" type="submit" value="Speichern" />
}
This ist my JS code:
function calculateRate() {
$.ajax({
type: "POST",
url: "@Url.Action("CalculateRate", "Marzipan")",
data: "test",
dataType: "text",
contentType: "text",
success: function (response) {
alert("success: " + response);
},
failure: function (response) {
alert("fail: " + response);
},
error: function (xhr, desc, error) {
window.alert('description: ' + desc);
window.alert('error: ' + error);
}
});
};
The JS function is called by a button in the html partial view.
This is my Controller code:
[HttpPost]
[ValidateAntiForgeryToken]
public string CalculateRate(string data)
{
data = "success!";
return data;
}