I have a table in my .aspx view in MVC3 project.
I am using .aspx views in MVC3 instead of Razor engine or .cshtml views.
I have the underwritten function in my jquery that gets me a JSON object from controller with some values in it.
function GetUsers() {
$.ajax({
url: ('/Home/GetUsers'),
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(),
success: function (result) {
alert(result.length);
var partnersTable = $('#PartnersTable');
partnersTable.html();
},
error: function () { alert("error"); }
});
}
Now I have a table in my view
<div id = "topGrid">
<table id="PartnersTable" style="float: left; width: 49%">
<th style="width: 75%">Partner</th>
<th style="width:25%">Users</th>
</table>
This is how I am getting the JSON object. right now its just dummy data but willb e populaed from DB later
public JsonResult GetUsers()
{
var model = new List<UsersModel>();
var item = new UsersModel();
for (int i = 1; i <= 10; i++)
{
item.Partner = "Partner" + Convert.ToString(i);
item.Count = i;
model.Add(item);
}
return Json(model, JsonRequestBehavior.AllowGet);
}
I need to show the data from above JSON object in my table.
how can I achieve this?
I am utterly new to MVC3 so please let me know if I have missed anything that is required to answer this question and please be as detailed as you can.
There are 2 approaches you might consider.
Let’s see the first approach:
Next you will have a corresponding partial view that will contain the respective section of the table:
and then inside your main view you will have the table:
and finally use AJAX to populate the body of the table:
Now let’s take a look at the second approach which consists into having the controller action return JSON and build the HTML template of the table manually:
and then: