Click here to Skip to main content
15,116,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a 2 tables as follows:

SQL
Users Table:-
UserID UserName       FirstName 
  1    ak@gmail.com    ArunK
  2    nk@gmail.com    NeelamK
Courses Table:-
CourseID CourseName       CourseDesc         Status CreatedBy CreatedOn
  10     MVC               MVC-Description   Draft      1     2013-09-24
  20     Cloud Computing   Cloud-Description Draft      2     2013-09-24

I have Model files respectively for both the tables as User.cs and Course.cs
I have a CourseController (Scaffolding options Selected as Model class = Course.cs AND Data context class = LMS_Global.cs) which has a Index ActionMetod :

namespace LMS.Controllers
{
    public class CourseController : Controller
    {
        private LMS_Global db = new LMS_Global();

        public ActionResult Index() 
       { return View(db.Course.ToList()); }
    }
}

Data context class as:
namespace LMS.Models
{
    public class LMS_Global : DbContext
    {
        public DbSet<User> User { get; set; }
        public DbSet<Course> Course { get; set; }
    }
}

I have got a view for ActionMethod Index as Index.cshtml :-
HTML
@model IEnumerable<LMS.Models.Course>

 @{int i = 1;}
 @foreach (var item in Model)
 {  
     <tr>
         <td>
              @(i++)
         </td>
     </tr>
     <tr>
         <td>
              @item.CourseName
         </td>
      </tr>
      <tr>
         <td>
             Drafted By: @item.CreatedBy on: @item.CreatedOn.ToString("dd MMMM yyyy")
         </td>
      </tr>
      <tr>
         <td>
              @item.Status
         </td>
      </tr>
 }


Now, In place of @item.CreatedBy(from Course Table), I want to display Corresponding FirstName for that CreatedBy. How can I display firstName from Users Table ? There is no relation between CreatedBy(Courses) and UserID(User). 
Quick help will be appreciated.
Thanks in advance.


- Shweta K.
Posted
Updated 25-Sep-13 1:27am
v2
Comments
bbirajdar 25-Sep-13 6:14am
   
Simple....If there is no relation, then you cannot display..
shwetavc30 25-Sep-13 6:18am
   
I need a solution. So, can you please give me a solution corresponding to the mentioned scenario ?

1 solution

you can select first name of user by using linq.
Create a new ViewModel class like this
C#
public class CourseViewModel
   {
       public int CourseId { get; set; }

       public string CourseName { get; set; }

       public string CourseDesc { get; set; }

       public string Status { get; set; }

       public int CreatedBy { get; set; }

       public DateTime CreatedOn { get; set; }

       public string FirstName { get; set; }
   }


And in Controller pass this list to view,which is strongly binded to IEnumerable<courseviewmodel>,so that in view @item.FirstName display name of user.

C#
var listOfModel =  from course in courses
                  let firstOrDefault = users.FirstOrDefault(x => x.Id == course.CreatedBy)
                   where firstOrDefault != null
                   select new CourseViewModel()
                    {
                      CourseId = course.CourseId,
                      CourseName = course.CourseName,
                      CourseDesc = course.CourseDesc,
                      Status = course.Status,
                      CreatedBy = course.CreatedBy,
                      CreatedOn = course.CreatedOn,
                      FirstName = firstOrDefault.FirstName
                    };
   
v4
Comments
shwetavc30 25-Sep-13 7:48am
   
Thanks a lot Pooja ! :) :)

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