Click here to Skip to main content
13,141,445 members (22,624 online)
Rate this:
 
Please Sign up or sign in to vote.
Hello,
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 20:11pm
Updated 24-Nov-13 20:13pm
v2

1 solution

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

Solution 1

Hi,

I think you are using https://gridmvc.codeplex.com/wikipage?title=Quick%20Start&referringTitle=Home[^] grid.

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

Example:

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.
</partyhomemodel></partyhomemodel>
  Permalink  

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 |
Web02 | 2.8.170915.1 | Last Updated 25 Nov 2013
Copyright © CodeProject, 1999-2017
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