1

I am trying to follow a few tutorials on how to do this but I am coming out with no results.

I have to use two models in my view and they're both retrieving data from the database but when I try to combine them into one view it says I'm getting a null result. How can I query from the database and return both models to the view?

User.cs

public class User
{
    [Key]
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string UserPassword { get; set; }
}

Course.cs

public class Course
{
    [Key]
    public int CourseID { get; set; }
    public string CourseName { get; set; }
    public string CourseDescription { get; set; }
}

CommonViewModel

public class CommonViewModel
{
    public List<User> users { get; set; }
    public List<Course> courses { get; set; }
}

DefaultConnection.cs

    public class DefaultConnection : DbContext
    {
        public DefaultConnection() : base("DefaultConnection") { }

        public DbSet<User> Users { get; set; }
        public DbSet<Course> Vehicles { get; set; }
    }

View

@model TEST.Models.CommonViewModel

@foreach (var item in Model.users)
{
    @Html.DisplayFor(modelItem => item.UserID)
}

HomeController

private DefaultConnection db = new DefaultConnection();

       public ActionResult Index()
        {
            List<User> users = new List<User>();

            List<Course> courses = new List<Course>();

            var viewModel = new CommonViewModel();
            viewModel.users = users;
            viewModel.courses = courses;

            return View(viewModel);
        }
4
  • Like the error says: Your CommonViewModel doesn't, in fact, have a definition for "users". It DOES have a definition for "user". Commented Nov 24, 2015 at 17:43
  • Ah I got it now, but now I get an error saying it is null, do I have to add CommonViewModel to my dbcontext or something? Commented Nov 24, 2015 at 17:52
  • CommonViewModel's properties should be public & List Commented Nov 24, 2015 at 18:00
  • I actually changed it to that a few minutes ago just forgot to update my question. It still says I have null values even though my database is filled with info. Commented Nov 24, 2015 at 18:01

2 Answers 2

1

I think you have not initialized CommonViewModel properly. Following code snippent may help you.

 public class HomeController
 {
    private DefaultConnection db = new DefaultConnection();

    public ActionResult Index()
    {
        var viewModel = new CommonViewModel();
        viewModel.users = db.User.ToList();
        viewModel.courses = db.Course.ToList();

        return View(viewModel);
    }        
}

See this dotnetfiddle. Hope this will help you.

https://dotnetfiddle.net/aqkE8v

Sign up to request clarification or add additional context in comments.

9 Comments

I already have a database full of information I dont need to add information.
Yes i know, you should your data instead of mine. Take a look to the link given.
Its still highlighting Model.users' and saying object reference not set to an instance of an object` How is this model pulling the data from the db? I don't see it in the controller code
I added your controller code exactly and now I'm not getting any errors but the page is blank.
As you updated your question it always be blank because your List are empty.
|
0

Either your model should contain the definition of Users; it's contain property named user.

public class CommonViewModel
{
    public List<User> Users {get ; set; }
    public List<Course> courses {get ; set; }
}

or, you should return the List from Action. In that case you should be changing as

@model List<TEST.Models.CommonViewModel>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.User.UserID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.User.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.User.UserPassword)
        </td>
    </tr>
}

3 Comments

User is highlighted with errors saying the model doesn't contain a definition for it and if I change it to users it says List<User> does not contain a definition for UserID
Foreach is done on Model.Users in your question. So item will have the required properties to display
users != Users. Make sure that you got the casing right, since it's bouncing around between your question and the answer here. FWIW, style guidelines state that properties should be title-cased: Users, not users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.