[RESOLVED]How to create a strongly type view when I have join in query which I am using in Action method?

I am working with a action method in which I add a linq query with joins. Now I want to add view for this action method. But the problem is this the linq query is getting data from two entity models and I want to select strongly type view then which class
I  have to add for this. Here is the Action Method

 public ActionResult Marks(int id)
        {
            var marksjoin = (from a in db.TbStudent
                             join b in db.TbMarks on a.StudentId equals b.StudentId
                             select new
                                 {

                                     a.StudentName,
                                     b.StudentId,
                                     b.Hindi,
                                     b.English,
                                     b.SocialStudy,
                                     b.Science,
                                     b.Maths,
                                     b.Total
                                 }).ToList();

           // var marks = db.TbMarks.Where(m => m.StudentId == id).SingleOrDefault();
            if (marksjoin == null)
            {
                return RedirectToAction("PostMarks");
            }
            else
            {
                return View(marksjoin);
            }

Now I am getting the data from two entity models and how can I create a strongly type view for this ?

Hi,

You have to create a new viewModel dedicated to ur view.

select new myNewModel{
					 a.StudentName,
					 b.StudentId,
					 b.Hindi,
					 b.English,
					 b.SocialStudy,
					 b.Science,
					 b.Maths,
					 b.Total
				 }).ToList();

other than that in don’t think of any other alternatives.

you need to create another model class which have properties of bothe student and marks. Data model and view model will always be different. Data entity model would be based on the actual table. View model should be based on the details you need to show
in page. If you want to do pagination and search you need to add those properties to  your view model as well.

The answer would be to create ViewModel, TbStudentView which will have the properties from your Select statement:

StudentName,
StudentId,
Hindi,
English,
SocialStudy,
Science,
Maths,
Total

In view you can use, like

@model Models.TbStudentView

Rajesh Mishra

The answer would be to create ViewModel, TbStudentView which will have the properties from your Select statement:

StudentName,
StudentId,
Hindi,
English,
SocialStudy,
Science,
Maths,
Total

In view you can use, like

@model Models.TbStudentView

Can you please give me complete example.. I am newbie in MVC

make a new directory in your mvc project called ViewModels. 

add a new class to this folder and call it StudentVM.cs then assign the properties you need to your viewmodel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyMvcProject.ViewModels
{
    public class StudentVM
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    }
}

…. 

You need 3 things to do the job

1. Action Method which will fetch data applying joins, such as:

public ActionResult Marks(int id)
        {
            MyViewModel viewmodel= (from a in db.TbStudent
                             join b in db.TbMarks on a.StudentId equals b.StudentId
                             select new
                                 {

                                     a.StudentName,
                                     b.StudentId,
                                     b.Hindi,
                                     b.English,
                                     b.SocialStudy,
                                     b.Science,
                                     b.Maths,
                                     b.Total
                                 }).ToList();

           // var marks = db.TbMarks.Where(m => m.StudentId == id).SingleOrDefault();
            if (marksjoin == null)
            {
                return RedirectToAction("PostMarks");
            }
            else
            {
                return View(viewmodel);
            }
}

2. Create a class in Model or some abc New folder, and name it ViewModel.cs, which will have following properties"

public int StudentId { get; set; }
public string StudentName { get; set; }
Hindi,
English,
SocialStudy,
Science,
Maths,
Total

The 3rd and the last thing is, using it in view as strongly typed:

@model Models.ViewMode

@Html.TextBoxFor(model=>model.StudentId)
@Html.TextBoxFor(model=>model.StudentName)

and so on.....

raju dasa

Hi,

You have to create a new viewModel dedicated to ur view.

select new myNewModel{
					 a.StudentName,
					 b.StudentId,
					 b.Hindi,
					 b.English,
					 b.SocialStudy,
					 b.Science,
					 b.Maths,
					 b.Total
				 }).ToList();

other than that in don’t think of any other alternatives.

Thanks for your solution but it took one day  to understand me this Tongue Out. But at last I successfully did this  

Leave a Reply