Click here to Skip to main content
11,932,103 members (54,605 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Gridview MVC3 Razor
I am working an MVC3 application where I need to make use of the MVC Grid.
Now as I am not abale to bind my datatable with the MVC grid. beloow is the controller code using which I am displaying certain data in the pages. It has a model name PartyHomeModel. There's a datatable in the model along with other variables. I am filling these from my database as given below.:-
public ActionResult PartyHome()
DataManager DM = new DataManager();
PartyHomeModel pmodel=new PartyHomeModel();
string SQL = " select p.*,u.mines_name from PARTY_MASTER p inner join UNIT_MASTER as u on p.MINES_CODE=u.ore_mines_code where p.PARTY_CODE='" + Session["user_ID"] + "' and p.MINES_CODE='" + Session["mines"] + "' ";
DataTable DT2 = new DataTable();
DT2 = DM.GetDataTable(SQL);
pmodel.MINES_CODE = DT2.Rows[0]["MINES_CODE"].ToString();
pmodel.PARTY_CODE = DT2.Rows[0]["PARTY_CODE"].ToString();
pmodel.mines_name = DT2.Rows[0]["mines_name"].ToString();
pmodel.PAN = DT2.Rows[0]["PAN"].ToString();
pmodel.PARTY_NAME = DT2.Rows[0]["PARTY_NAME"].ToString();
pmodel.PHONE = DT2.Rows[0]["PHONE"].ToString();
pmodel.REGN_NO = DT2.Rows[0]["REGN_NO"].ToString();
pmodel.EMAIL = DT2.Rows[0]["EMAIL"].ToString();
pmodel.ADDRESS4 = DT2.Rows[0]["ADDRESS4"].ToString();
pmodel.ADDRESS3 = DT2.Rows[0]["ADDRESS3"].ToString();
pmodel.ADDRESS2 = DT2.Rows[0]["ADDRESS2"].ToString();
pmodel.ADDRESS1 = DT2.Rows[0]["ADDRESS1"].ToString();

SQL = " select RecordSource ,BillNo ,RecordNo,convert(varchar(11),RecordDt,106) as RecordDt,BillStatus,convert(varchar(11),BillDt,106) as BillDt,BillAmt,PayMode,ChequeNo,convert(varchar(11),ChequeDt,106) as ChequeDt from Tran_BillStatus where Party_Code='" + Session["user_ID"] + "' and Mines_Code='" + Session["mines"] + "' ";
pmodel.BillDT =DM.GetDataTable(SQL);
return View(pmodel);

As u see BillDT is the Datatable I am filling up and now i want to bind this to my Grid in my Html page. but the problem that I am facing is that as I am returning the model "pmodel" to the view and I am able to access and display all data but i cannot bind this datatable in the model "BillDT" to my Gridview. Kindly can u help me by showing the process in which a grid is bind with such datatable using MVC3 and razor tools.
Thank you
Posted 24-Nov-13 21:11pm
Edited 24-Nov-13 21:13pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


I think you are using[^] grid.

If you are using above grid, you have to bind data to view as a list not as a data table.


List<partyhomemodel> pmodel=new List<partyhomemodel>();
foreach(DataRow DR in DT2.Tables[0].Rows)
PartyHomeModel Item=new PartyHomeModel();
 // Get the data from your table and prepare object
pmodel.Add(PartyHomeModel ); //Assign object to the list
// Finally you have to return this list to the View instead of data table

return view(pmodel);
// In your model you can access this list and bind it to Your MVC grid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 25 Nov 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100