On a button click event i am trying to pass a textbox value to a controller . I am able to view the text box values by using console but in controller always returns null
this is my razor code
JavaScript
@{
ViewData["Title"] = "Index";
Layout = "Commonlayout";
}
$(document).ready(function () {
var lol = document.getElementById("report").value;
console.log(lol);
$("#generate").click(function () {
$.ajax({
type: "POST",
url: "/HRDashboard/getresult",
data: { //Passing data
name: $("#txtName").val()
}
});
});
});
@*Index*@
@*
Create New
*@
To be Validated
Report Typeand my controller is as follows
C#
public JsonResult getresult(string name)
{
}I am not sure where i am wrong please guide.

Vishal YelveRank #178 · 13.6K reputationPosted Mar 29, 2023, 9:33 AM EDT
try this
add dataType: "JSON"
$(document).ready(function () { var lol = document.getElementById("report").value; console.log(lol); $("#generate").click(function () { $.ajax({ type: "POST", dataType: "JSON" url: "/HRDashboard/getresult", data: { //Passing data name: $("#txtName").val() } }); }); });Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 29, 2023, 4:14 PM EDT
$.ajax({ type: 'POST', url: '/ControllerName/ActionName', data: { textBoxValue: $('#textBoxId').val() }, success: function(response) { // Do something with the response }, error: function(error) { // Handle any errors } });