TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Dharmraj Thakur
235
7.7k
706.6k
Best way to read from Datatable and return JsonResult IN MVC
Apr 29 2017 3:00 AM
Anyone suggest me what is the best way to read bult data from DataTable and return as Json Multidimensional array to view...
I am using currently following...
public
JsonResult GetAllCategory(
)
{
List<Dictionary<
string
,
object
>> datarow =
DataTableToList(
new
CategoryBAL().GetCategoryForEdit());
new
JavaScriptSerializer().Serialize(datarow);
return
Json(datarow, JsonRequestBehavior.AllowGet);
}
It will getting all rows from database and call the DataTableToList() to convert data into List<Dictionary<string,object>>
public
List<Dictionary<
string
,
object
>> DataTableToList(DataTable dt)
{
List<Dictionary<
string
,
object
>> parentRow =
new
List<Dictionary<
string
,
object
>>();
foreach
(DataRow row
in
dt.Rows)
{
Dictionary<
string
,
object
> childRow =
new
Dictionary<
string
,
object
>();
foreach
(DataColumn col
in
dt.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return
parentRow;
}
Is there any other faster and optimized way to do this?
Reply
Answers (
1
)
How to print in this pattern
How to fill DataGridView Cells on cellvaluechanged in MYSQL