Hi nabilabolo,
Check this example. Now please take its reference and correct your code.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult GetFail()
{
string fail = "";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT fail FROM Fail WHERE fail_id = @Id";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", 12);
con.Open();
fail = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
return Json(fail, JsonRequestBehavior.AllowGet);
}
}
View
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "/Home/GetFail",
data: {},
success: function (response) {
if (response != "") {
$('#txtFail').val(response);
} else {
$('#txtFail').val('');
}
}
});
});
</script>
<%:Html.TextBox("txtFail") %>