I do not know what the problem is, that I can't connect to my Webservice. It says error 404. As far as I can tell it should work. Since I removed the Code from the *.cshtml in to a seperate .js file its not working anymore.

The Javascript file is in a subfolder "Scripts/Reps/report.ui.js"

I'd like to call the method GetReport from The HomeController.cs with the standard configuration.

Can you tell me what I'm doing wrong, or if I totally missunderstood that part? Thank you for helping me

$("#createReport").click(function () {
    var postdata = JSON.stringify(
        {
            "EvaluatedPrice": $("#estimatedPriceValue").text(),
            "Address": $("#address").text(),
            "SomeValue": "Som RandomValue To test"
        });

    try {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("GetReport", "Home")',
            data: postdata,
            dataType: "json",
            success: getSuccess,
            error: getFail
        });
    }
    catch (e) {
        alert(e);
    }
});

function getSuccess(data, textStatus, jqXHR) {
    alert(data.Response);
}

function getFail(jqXHR, textStatus, errorThrown) {
    alert(jqXHR.status);
}
public class HomeController : Controller
    {
[HttpPost]
        [Route("GetReport")]
        public async Task<ActionResult> GetReport(HttpContext context)
        {
            string jsonString = string.Empty;

            var data = context;

            return Json(new { data = 12 });
        }
    }
}

1 Answers

3
imk On Best Solutions

@Url.Action("GetReport", "Home") it will not work in a *.js file. razor syntax only works in *.cshtml file. yout need to store that url in a variable.