Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends

these are my model classes

public partial class tbl_staticfielddetails
{
public Nullable<int> institutionid { get; set; }
public string fieldname { get; set; }
public string required { get; set; }
public string predefinedvalue { get; set; }
public string fieldpattern { get; set; }
public string errormsg { get; set; }
public string minimum { get; set; }
public string maximum { get; set; }
public int fieldorder { get; set; }
public string tooltype { get; set; }
public string datatype { get; set; }
public string disptype { get; set; }
public int id { get; set; }
public string status { get; set; }
public string label { get; set; }
public string instructuser { get; set; }
}

public partial class tbl_student
{
public int Stud_id { get; set; }
public int institutionid { get; set; }
public string firstname { get; set; }
public string middlename { get; set; }
public string lastname { get; set; }
public string studentimage { get; set; }
public Nullable<long> admissionnumber { get; set; }
public string admissionforclass { get; set; }
public Nullable<system.datetime> dateofadmission { get; set; }
public Nullable<long> boardregisternumber { get; set; }
public Nullable<system.datetime> dateofbirth { get; set; }
public string gender { get; set; }
public string isactive { get; set; }

}

below are two tables corresponding to models.

http://sqldiscussion.com/wp-content/uploads/2014/08/WebGrid-Image.png[^]

please refer the image

i want to display table 2 records in webgrid

but its column header is refered from table 1's label field.

my requirement is how to design the model class as well as controller method to display records in webgrid .

thanks in advance.
Posted
Updated 3-Dec-19 17:49pm
v2

Build a ViewModel that incorporates the two. You can pull both from your repository in the controller and use a constructor overload for the objects.

Then just return List<myviewmodel> and build a grid from that.


C#
public class MyViewModel
{
    public tbl_student Student { get; set; }

    //any label properties needed

    public MyViewModel(tbl_staticfielddetails details, tbl_student student)
    {
        Student = student;
        //set appropriate properties from details
    }
}
 
Share this answer
 
hi Nathan Minier,

i got the answer.

thanks for your reply.

http://forums.asp.net/t/2000875.aspx?Problem+Display+Records+in+Webgrid
 
Share this answer
 
v2
I have faced the same issue and I have find the solution. I am using the WebGrid to bind the data from two tables and display in a single page.
C#
public class HomeController : BaseController
    {
        [HttpGet]
        public async Task<actionresult> Index()
        {

            string loginUserId = TempData[WebConstants.LoginUserId].ToString();
            string loginUserName = TempData[WebConstants.LoginUserName].ToString();
            string authmode = TempData[WebConstants.AuthenticationMode].ToString();
            HttpResponseMessage response_Cat = null;
            HttpResponseMessage response_Printer = null;
            IEnumerable<catridge> list_Cat = null;
            IEnumerable<printer> list_Printer = null;

            if (loginUserId == "1" && authmode == "Admin")
            {
                CustomLogger.Write("Service Call: Get All Catridge: Start");
                response_Cat = await ServiceGet(ClientConfig.ServiceLayerUrl + ClientConfig.GetAllCatridgeUrl);
                CustomLogger.Write("Service Call: Get All Catridge: End");

                var result_Cat = response_Cat.Content.ReadAsStringAsync().Result;
                list_Cat = JsonConvert.DeserializeObject<ienumerable<catridge>>(result_Cat).OrderByDescending(s => s.Id);                    


                CustomLogger.Write("Service Call: Get All Printer: Start");
                response_Printer = await ServiceGet(ClientConfig.ServiceLayerUrl + ClientConfig.GetAllPrinterUrl);
                CustomLogger.Write("Service Call: Get All Printer: End");

                var result_Printer = response_Printer.Content.ReadAsStringAsync().Result;
                list_Printer = JsonConvert.DeserializeObject<ienumerable<printer>>(result_Printer).OrderByDescending(s => s.Id);                    

                dynamic mymodel = new ExpandoObject();
                mymodel.Catridge = list_Cat;
                mymodel.Printer = list_Printer;
                return View(mymodel);

            }


            else
            {
                CustomLogger.Write("Service Call: GetAllPrinters: Failed|" + response_Printer);
                ModelState.AddModelError(Resources.Resource.MasterError, Resources.Resource.UnableToProcessRequest);
                list_Printer = new List<printer>();
                return View(list_Printer.AsEnumerable());
            }

        }

    }
Now, I have binded the controller result data to WebGrid in MVC.

MVC view Name : Index.cshtml
HTML
@model dynamic   


<div class="container">
    <div class="row">
        <h1 class="mainHeading col-md-12">Cartridge List</h1>
    </div>  

    <div class="row">
        @{
            WebGrid grid_Cart = new WebGrid(Model.Catridge, canPage: true, rowsPerPage: 5);
            @grid_Cart.GetHtml(
         htmlAttributes: new { id = "GridTable_Cart" },
         tableStyle: "table table-bordered table-striped",

        columns: new[] {
grid_Cart.Column("TeamName",header: "Team Name", canSort:false,style : "gridTableID"),
grid_Cart.Column("PartNo",header: "PartNo", canSort:false,style : "gridTableID"),
grid_Cart.Column("PrinterName",header: "Printer Name", canSort:false,style : "gridTableID"),
grid_Cart.Column("LoginUserName",header: "Requester Name", canSort:false,style : "gridTableID"),
grid_Cart.Column("CatridgeColor",header: "Cartridge Color", canSort:false,style : "gridTableID"),
grid_Cart.Column("CatridgeDrumsColor",header: "DrumsColor", canSort:false,style : "gridTableID"),
grid_Cart.Column("Others",header: "Others", canSort:false,style : "gridTableID"),
grid_Cart.Column("AssigneeName",header: "Assignee", canSort:false,style : "gridTableID"),
grid_Cart.Column("Status",header: "Status", canSort:false,style : "gridTableID")
     })
        }

    </div>
</div>

<div class="container">
    <div class="row">
        <h1 class="mainHeading col-md-12">Printer List</h1>
    </div>

    <div class="row">
        @{
            WebGrid grid_printer = new WebGrid(Model.Printer, canPage: true, rowsPerPage: 5);
            @grid_printer.GetHtml(
    htmlAttributes: new { id = "GridTable_Printer" },
    tableStyle: "table table-bordered table-striped",

    columns: new[] {
    grid_printer.Column("TeamName",header: "Team Name", canSort:false,style : "gridTableID"),
    grid_printer.Column("PrinterName",header: "Printer Name", canSort:false,style : "gridTableID"),
    grid_printer.Column("Version",header: "Version", canSort:false,style : "gridTableID"),
    grid_printer.Column("Bundle",header: "Printer Bundle", canSort:false,style : "gridTableID"),
    grid_printer.Column("PrinterFloorNo",header: "FloorNo", canSort:false,style : "gridTableID"),
    grid_printer.Column("PrinterDeskNo",header: "DeskNo", canSort:false,style : "gridTableID"),
    grid_printer.Column("AssigneeName",header: "Assignee", canSort:false,style : "gridTableID"),
    grid_printer.Column("Status",header: "Status", canSort:false,style : "gridTableID")
                })
        }

    </div>
</div>
Please let us know if anyone need any more clarification.
 
Share this answer
 
v2
Comments
phil.o 4-Dec-19 3:32am    
I seriously doubt anyone will want to go through this pile of code to try and find it any practical meaning. A code dump, let alone a huge one, is useless without proper explanation and linking to actual context.
CHill60 4-Dec-19 3:33am    
I've tidied his code up for him. Saw the report in the S & A forum which I thought was a bit harsh.
phil.o 4-Dec-19 3:58am    
It was harsch indeed. I did not report the user nor the solution, but I think this kind of answers not related to OP's context are not helpful to anyone.
CHill60 4-Dec-19 3:32am    
I believe you are getting downvoted because your solution looked like an unexplained code dump... and the original question is rather old. I've tried to format your solution to make it more presentable, and to highlight your commentary.
When you paste anything into a solution you are offered a pop-up menu that will automatically format code, quotes etc. You should use it. Alternatively, you can highlight sections of your solution and use the menu at the top of the input box to format code sections.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900