There are two array lists, RoleList and EmpList. How can I return both arrays in JSON format and display them in rows on a data table?
In order to return both arrays, you can use the following code snippet:
return Json(EmpList,RoleList, JsonRequestBehavior.AllowGet);
[HttpPost]
[MyExceptionHandler]
public ActionResult ViewModules(int id)
{
Domain_Bind();
dynamic mymodel = new ExpandoObject();
userType type = new userType();
List<ViewRoleModules> EmpList = type.GetRoleModulesViews(id);
string sRptModuleIDs = string.Empty;
foreach (ViewRoleModules emp in EmpList)
{
sRptModuleIDs += emp.ModuleID + ",";
}
if (sRptModuleIDs != "")
{
sRptModuleIDs = sRptModuleIDs.Remove(sRptModuleIDs.Length - 1, 1);
}
List<ViewRoleModules> RoleList;
foreach (var rid in sRptModuleIDs.Split(','))
{
string RID = rid;
RoleList = type.GetSiteRoleModulesViews(rid);
}
return Json(EmpList, JsonRequestBehavior.AllowGet);
}
Scripts:
<script>
$(document).ready(function () {
$("#DomainID").change(function () {
var id = $(this).val();
$("#example tbody tr").remove();
$.ajax({
type: 'POST',
url: '@Url.Action("ViewModules")',
dataType: 'json',
data: { id: id },
success: function (data) {
var items = '';
$.each(data, function (i, item) {
$("#findValue").show();
var rows = "<tr>"
+ "<td>" + i + "</td>"
+ "<td>" + item.ModuleName + "</td>"
+ "<td>" + item.Url + "</td>"
+ "<td>" + item.RoleName + "</td>"
+ "</tr>";
$('#example tbody').append(rows);
});
},
error: function (ex) {
var r = jQuery.parseJSON(response.responseText);
alert("Message: " + r.Message);
alert("StackTrace: " + r.StackTrace);
alert("ExceptionType: " + r.ExceptionType);
}
});
return false;
})
});
</script>
Cshtml:
<table id="example" class="display table table-bordered" cellspacing="0" width="100%;">
<thead>
<tr>
<th>S#</th>
<th>Module Name</th>
<th>Url</th>
<th>Roles</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
The current code only returns one array list, but you need to display two array lists.
To achieve this, you can use the following methods:
public List<ViewRoleModules> GetRoleModulesViews(int id)
{
// Code for getting and formatting EmpList
}
public List<ViewRoleModules> GetSiteRoleModulesViews(string rid)
{
// Code for getting and formatting RoleList
}