أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.
AJax doesn't work If not included in .CSHTML file. I would like to include Ajax in .JS file in .Net MVC.
To use Ajax in a separate jQuery file in a .NET Core MVC application, you can follow these steps:
1- Create a new jQuery file: First, create a new jQuery file in your project and add the necessary code to make an Ajax request. For example:
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/Home/GetData",
success: function (data) {
// code to handle the response data
}
});
});
2- Link the jQuery file in your view: Next, link the jQuery file in your view using the script tag. For example:
<script src="~/path/to/jquery.js"></script>
3- Add a controller action to handle the Ajax request: In your .NET Core MVC application, add a controller action to handle the Ajax request made by the jQuery file. This action should return the data that you want to send back to the client. For example:
public class HomeController : Controller
{
[HttpGet]
public JsonResult GetData()
{
// retrieve data from the database or other source
var data = // data retrieved from the database
return Json(data);
}
}
4- Test the Ajax request: Finally, test the Ajax request by running the application and verifying that the data is returned as expected.
I hope this helps! Let me know if you have any questions.