0

I'm having trouble figuring out how to do this. The examples out there are not helping me understand what I need.

View Model

public class ViewModelAddUser 
{
    StudentSchedulingDBContext ssDB = new StudentSchedulingDBContext();

    public User User { get; private set; }
    public SelectList Departments { get; private set; }

    public ViewModelAddUser()
    {
        // Not Sure what to put here
    }


}

DBContext of Table (Just to show table structure in code)

public class Department
{
    public int DepartmentID { get; set; }
    public string DepartmentName { get; set; }
}

Controller

public ActionResult AddUser()
    {

        return View(new ViewModelAddUser());


    }

I'm not quite sure how to form the rest. I've been trying the nerddinner and other questions, but I'm not getting this to work.

Update

**View Model:**


public class ViewModelAddUser 
{
    public IEnumerable<SelectListItem> Departments { get; set; }
}

Controller:

public ActionResult AddUser()
    {
        ViewModelAddUser model = new ViewModelAddUser()
        {
            Departments = db.Departments.Select(department => new SelectListItem {
                Value = department.DepartmentID.ToString(),
                Text = department.DepartmentName
            })
        };
        return View(model);  
    }
xivo
  • 2,054
  • 3
  • 22
  • 37
  • Check out this answer: http://stackoverflow.com/questions/6623700/how-to-bind-a-selectlist-with-viewmodel – Austin Thompson Apr 27 '12 at 19:48
  • Yeah, that doesn't really help me. Its like the other examples that I tried. I have a feeling its the way I mapped my table or something I am missing. – xivo Apr 27 '12 at 21:51

1 Answers1

3

A View model should only contain the elements that will be used within the view. That shouldn't include your db context. Likewise, make sure the user class you mention in the view model, is a view model for creating a user, and not your user domain model.

I think your view model should probably look something like :

public class ViewModelAddUser 
{
    public UserViewModel User { get; set; } //depending on what your doing a string containing UserId might suffice here
    public IEnumerable<SelectListItem> Departments { get; set; }
    public string DepartmentId { get; set; } // this will be the department set by drop down list
}

To create the dropdownlist:

StudentSchedulingDBContext ssDB = new StudentSchedulingDBContext(); //I'm assuming this can be queried to get your departments.
ViewModelAddUser model = new ViewModelAddUser()
{
    Departments = ssDB.Departments.Select(department => new SelectListItem {
         Value = department.departmentID,
         Text = department.departmentName
    }),
    User = //set user here if necessary
}

return View(model);

To call the drop down list:

@Html.DropDownListFor(m => m.DepartmentId, Model.Departments);

Also check out the link Austin provided, Darin's answer on that question might help you.

DMulligan
  • 8,993
  • 6
  • 33
  • 34
  • That doesn't seem to work for me. I don't understand where that goes and why mine is wrong since my DBContext is just showing how I mapped the table. – xivo Apr 27 '12 at 21:51
  • What exactly isn't working? Note my code probably wouldn't work verbatim since I don't know what studentschedulingDBContext is. I assumed it contained a list of your departments in a manner similar to the way I used it. – DMulligan Apr 27 '12 at 22:01
  • I get a runtime error: The model item passed into the dictionary is of type 'StudentScheduling.Models.ViewModelAddUser', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[StudentScheduling.Models.ViewModelAddUser]'. – xivo Apr 27 '12 at 22:09
  • Look at your @model statement in your view. I think you might be specifying "@model IEnumerable instead of "@model ViewModelAddUser" – DMulligan Apr 27 '12 at 22:12
  • @model StudentScheduling.Models.ViewModelAddUser is my specification, but now I'm not sure what the corrent helper would be. – xivo Apr 27 '12 at 22:18
  • You mean the correct helper to call the drop down list? Edited answer to give an example of that. – DMulligan Apr 27 '12 at 22:29
  • LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. is what I'm getting now. I'm not quite sure what else I have to add or if I'm doing the pattern correctly. – xivo Apr 27 '12 at 22:36
  • I dont use Linq to Entities so can't help you. Try http://stackoverflow.com/questions/1228318/linq-int-to-string. – DMulligan Apr 27 '12 at 22:49
  • Thanks for your help. I figured out what I was missing. – xivo Apr 28 '12 at 00:17