Article

MVC table data view using LINQ.

08 Jun 2018 Naman
0 Comments 743 Views
MVC



1. First method single LINQ List in a MVC razor page which is direct Listed loop

2.Second method class contains list which in turn is called rather than calling list.

Code Flow:-

MVC razor page(.cshtml) -- Controller -- Model
Here Data is retrieved from Model to Controller then to .cshtml page using LINQ

I have created Index.cshtml page which contains

@model List<MVCTestApp.Models.TestModel> // which is used to get LINQ list directly.

and another page IndexA.cshtml

@model MVCTestApp.Models.TestModel

both gets list difference is in the call way.

First one get list directly and another one gets Model within its list.

Here first loop of Index page is this

@model List<MVCTestApp.Models.TestModel> //Direct List

@foreach (var item in Model)
{
    <tr>
       <td>@item.Name</td>
       <td>@item.EmailId</td>
    </tr>
}

and second loop of IndexA page is this

@model MVCTestApp.Models.TestModel //List within model

@foreach (var item in Model.OuterModel) //OuterModel List is in TestModel class
{
    <tr>
       <td>@item.Name</td>
       <td>@item.EmailId</td>
    </tr>
}

Controller call two methods for 1st and 2nd page


Using using MVCTestApp.Models;   // Its using namespace of model to call properties and model/lists.

1st Page with List directly

public ActionResult Index()
{      
    return View(new TestModel().ViewData());
}

2nd Page with Model within its List

public ActionResult IndexA()
{
    return View(new TestModel().InnerListData());
}

Model Page
TestModel.cs page

namespace MVCTestApp.Models
{
    public class TestModel
    {
        public string Name { get; set; }
        public string EmailId { get; set; }
        public List<TestModel> OuterModel { get; set; }

        //Basic List Call
        public List<TestModel> ViewData()
        {
            List<TestModel> _TestModelList = new List<TestModel>();

            TestModel _TestModel = new TestModel();

            //First List
            _TestModel = new TestModel();
            _TestModel.Name = "Dang";
            _TestModel.EmailId = "Dang@ymail.com";
            _TestModelList.Add(_TestModel);

            //Second List
            _TestModel = new TestModel();
            _TestModel.Name = "Tang";
            _TestModel.EmailId = "Tang@ymail.com";
            _TestModelList.Add(_TestModel);

            return _TestModelList;
        }

        //List within List Call
        public TestModel InnerListData()
        {
            TestModel _TestModel = new TestModel();

            List<TestModel> _TestModelList = new List<TestModel>();

            //First List
            _TestModel = new TestModel();            
            _TestModel.Name = "Dang";
            _TestModel.EmailId = "Dang@ymail.com";
            _TestModelList.Add(_TestModel);

            //Second List
            _TestModel = new TestModel();            
            _TestModel.Name = "Tang";
            _TestModel.EmailId = "Tang@ymail.com";
            _TestModelList.Add(_TestModel);

            _TestModel.OuterModel = _TestModelList;

            return _TestModel;
        }

    }
}

 

Comments

No coments found to display!

Leave a Comment