Display Multi-pal List in Single View in c# MVC

 Display Student Details and Student Marks in Single view


  • StudentMarksViewModel 


public class StudentMarksViewModel
    {
        public int Id { get; set; }
        public Nullable<int> Maths { get; set; }
        public Nullable<int> Phy { get; set; }
        public Nullable<int> chemestriy { get; set; }
    }


  • StudentDetailViewModel


public class StudentViewModel
    {
        public int StudentId { get; set; }
        public string Name { get; set; }
        public string Streem { get; set; }
        public string ContactNumber { get; set; }
        public string Address { get; set; }
    }


  • TwoListViewModel

 public class TwoListViewModel
    {
        public IEnumerable<Final_Form_Student> Final_Student_Form { get; set; }
        public IEnumerable<StudentMarksViewModel> StudentMarks { get; set; }
    }


  • Controller Code
public ActionResult Index()
        {
            TwoListViewModel MyviewModel = new TwoListViewModel();
            var MarksList = _context.StudenMarks.Select(x => new StudentMarksViewModel()
            {
                Id=x.Id,
                Maths=x.Maths,
                Phy=x.Phy,
                chemestriy=x.chemestriy,
            }).ToList();
            var stud_info = _context.StudentDetails.Select(x => new Final_Form_Student()
            {
                Name=x.Name,
                Streem=x.Streem,
                Address=x.Address,
                ContactNumber=x.ContactNumber
            }).ToList();
            MyviewModel.StudentMarks = MarksList;

            MyviewModel.Final_Student_Form = stud_info;
            return View(MyviewModel);
        }



  • View
@model DisplayMultipal_List_in_Single_view.Models.TwoListViewModel
@{
    ViewBag.Title = "Index";
}
<h3>Student Marks </h3>

<table class="table">
    <tr>
        <th>
            Maths
        </th>
        <th>
            Phy
        </th>
        <th>
            Chemestriy
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model.StudentMarks)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Maths)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Phy)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.chemestriy)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>

<br />
<br />
<h3>Student Detail</h3>
<table class="table">
    <tr>
        <th>
           id
        </th>
        <th>
           Name
        </th>
        <th>
            Streen
        </th>
        <th>
            Contact Number
        </th>
        <th>
            Student Address
        </th>
        <th></th>
     
    </tr>

    @foreach (var item in Model.Final_Student_Form)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.StudentId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Streem)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContactNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }

</table>

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

How To insert Radio Button Data in DataBase -- MVC

Toolbox Webparts Controls