Hi, I'm working with ASP.Net / SqlDataReader / C # and razor I have the sig. complication ...
I'm trying to show a table using AJAX but I'm not sure I'm doing it right, so I understand I have to make a method for my query to be stored in a list, I do not know if this list should go in the controller or in My model. At this moment I have only got the printed answer, but I need to remove the JSON format and put it in a table. I would greatly appreciate some help or illustration.
This is what I have.
<script src="~/Scripts/jquery-1.10.2.js"></script>
<div id="result"></div>
<input type="button" name="name" value="try me" onclick="DepListQuery()" />
<script>
function DepListQuery() {
$.ajax({
type: 'GET',
url: '@Url.Action("GetData","Home")',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#result').text(response.message);
},
failure: function (response) {
alert("hubo un error del diablo u.u");
}
});
}
</script>
You are my controller
//GetData function, simple ajax
[HttpGet]
public JsonResult GetData()
{
string stdb = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";
SqlConnection conn = new SqlConnection(stdb);
string sql = "SELECT *FROM[DB_PCC].[dbo].[Departments]";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
return Json(new { success = true, message = rd },
JsonRequestBehavior.AllowGet);
}