Saturday, 15 May 2010

c# - Paging In MVC Table Grid -



c# - Paging In MVC Table Grid -

what trying accomplish include paging functionality in grid(generated html table , foreach).

i not allowed utilize entity framework, webgrid, or other 3rd party grid.

below details of task:

demotestdata.cs file

namespace demo.data { public class demotestdata { demotestmodel objdemotestmodel = null; public string connectionstring { get; set; } public sqlconnection objconnection { get; set; } public sqlcommand objcommand { get; set; } public sqldatareader objreader { get; set; } public list<demotestmodel> getalldemotest() { list<demotestmodel> listdemotest = new list<demotestmodel>(); seek { objconnection = new sqlconnection(configurationmanager.connectionstrings["conn"].connectionstring); objconnection.open(); objcommand = new sqlcommand("sp_demotest", objconnection); objcommand.commandtype = system.data.commandtype.storedprocedure; objreader = objcommand.executereader(); if (objreader.hasrows) { while (objreader.read()) { // list populated here. objdemotestmodel = new demotestmodel(); objdemotestmodel.id = convert.toint32(objreader["id"].tostring()); objdemotestmodel.column1 = objreader["column1"].tostring(); objdemotestmodel.column2 = objreader["column2"].tostring(); objdemotestmodel.column3 = objreader["column3"].tostring(); objdemotestmodel.column4 = objreader["column4"].tostring(); objdemotestmodel.column5 = objreader["column5"].tostring(); objdemotestmodel.column6 = objreader["column6"].tostring(); listdemotest.add(objdemotestmodel); } //return liststatemodel; } } grab (exception ex) { string m = ex.message; } { if (objconnection != null) { objconnection.close(); } } homecoming listdemotest; } } }

sp_demotest procedure

create procedure [dbo].[sp_demotest] begin select id,column1,column2,column3,column4,column5,column6 dbo.demotest end

demotestmodel.cs file

namespace demo.entities { public class demotestmodel { public int id { get; set; } public string column1 { get; set; } public string column2 { get; set; } public string column3 { get; set; } public string column4 { get; set; } public string column5 { get; set; } public string column6 { get; set; } } }

demotestcontroller.cs file

namespace sampleproject.controllers { public class demotestcontroller : controller { // // get: /demotest/ //test tst = new test(); demotestdata info = new demotestdata(); //list<countrymodel> listmodel = new list<countrymodel>(); list<demotestmodel> listmodel = new list<demotestmodel>(); //demotestmodel demotestmodel = new demotestmodel(); public actionresult index() { listmodel = data.getalldemotest(); homecoming view(listmodel); } } }

index.cshtml file

@model ienumerable<demo.entities.demotestmodel> @{ viewbag.title = "index"; } <h2>index</h2> <table> <tr> <th> @html.displaynamefor(model => model.column1) </th> <th> @html.displaynamefor(model => model.column2) </th> <th> @html.displaynamefor(model => model.column3) </th> <th> @html.displaynamefor(model => model.column4) </th> <th> @html.displaynamefor(model => model.column5) </th> <th> @html.displaynamefor(model => model.column6) </th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @{ if (item.column1 == "textbox") { @html.textboxfor(modelitem => item.column1) } else if (item.column1 == "dropdown") { @html.dropdownlistfor(modelitem => item.column1, enumerable.empty<selectlistitem>()) } else { @html.displayfor(modelitem => item.column1) } } </td> <td> @{ if (item.column2 == "textbox") { @html.textboxfor(modelitem => item.column2) } else if (item.column2 == "dropdown") { @html.dropdownlistfor(modelitem => item.column2, enumerable.empty<selectlistitem>()) } else { @html.displayfor(modelitem => item.column2) } } <td> @{ if (item.column3 == "textbox") { @html.textboxfor(modelitem => item.column3) } else if (item.column3 == "dropdown") { @html.dropdownlistfor(modelitem => item.column3, enumerable.empty<selectlistitem>()) } else { @html.displayfor(modelitem => item.column3) } } </td> <td> @{ if (item.column4 == "textbox") { @*@html.textboxfor(modelitem => item.column4)*@ @html.textbox("test", "", new { style = "width:130px;" }) } else if (item.column4 == "dropdown") { @html.dropdownlistfor(modelitem => item.column4, enumerable.empty<selectlistitem>()) } else { @html.displayfor(modelitem => item.column4) } } </td> <td> @{ if (item.column5 == "textbox") { @*@html.textboxfor(modelitem => item.column5)*@ @html.textbox("test1", "", new { style = "width:130px;" }) } else if (item.column5 == "dropdown") { @*@html.dropdownlistfor(modelitem => item.column5, enumerable.empty<selectlistitem>())*@ @html.dropdownlistfor(modelitem => item.column5, enumerable.empty<selectlistitem>(), new { style = "width: 130px" }) } else { @html.displayfor(modelitem => item.column5) } } </td> <td> @{ if (item.column6 == "textbox") { @html.textboxfor(modelitem => item.column6) } else if (item.column6 == "dropdown") { @html.dropdownlistfor(modelitem => item.column6, enumerable.empty<selectlistitem>()) } else { @html.displayfor(modelitem => item.column6) } } </td> </tr> } </table>

(if..else status used generate dynamic controls)

this rendered view

it nice if provide solution.

c# .net asp.net-mvc

No comments:

Post a Comment