I am a beginner in ASP and facing an issue while attempting to pass a string from my JavaScript code to my controller. The intention is to utilize this string for querying my database.
JavaScript
function findEmployees(userCounty) {
$.ajax({
type: "POST",
dataType: "json",
url: '@Url.Action("Index", "Contact")',
data: JSON.stringify(userCounty),
contentType: "application/json",
success: function (response) {
alert(userCounty);
},
error: function (response) {
alert("failed");
}
});
}
Controller
[HttpPost]
public ActionResult Index (string userCounty)
{
var query = //implement linq query on the database
return View(query);
}
Currently, I only receive the "success" alert when using a JsonResult function in my Controller. However, I need it to eventually return a LINQ query with the View(); function which hasn't worked with a JsonResult function. Any suggestions or alternate approaches would be greatly appreciated. If there is a better way than ajax to pass the string stored in userCounty to my controller, please let me know.