3

I have a ViewModel as such:

public class EmployeeViewModel
{


    Employees employee{ get; set; }
    Budget budget { get; set; }



}

I like to pass this view model to a view that does a create that requires both of the tables.

I tried doing the following in the controller that would pass information to the view but was not successful:

    EmployeeViewModel pvm = new EmployeeViewModel()
    return View(pvm);

The reason being is that the Value of both employee and budget it null. How do I pass this information to the view so it knows about both of the tables?

2
  • 1
    Please don't take offense. I don't see any initialization of the Employees or Budget....is it happening in the constructor? Commented Oct 31, 2011 at 13:44
  • Can you give more information about your view? Are you getting page errors, or is nothing happening? When you say tables, I assume you mean the tables in the database that your Employees and Budget Models correspond to? when you say: "...does a create...", do you mean an insert into the database? Commented Oct 31, 2011 at 13:49

1 Answer 1

2

If you do this, you will have references in the ViewModel properties:

public class EmployeeViewModel
{
    Employees employee { get; set; }
    Budget budget { get; set; }

    public EmployeeViewModel()
    {
         employee = ... initialize Employee list/set...
         budget = ... initialize budget
    }

}

If Employees and Budgets are LINQ/ADO EF models, you could attach data from database in a controller:

public class BudgetController : Controller {

  public ViewResult Index() {
    var db = new YourContextClass();
    EmployeeViewModel pvm = new EmployeeViewModel();
    pvm.employees = db.Employees.All(); // or some where condition
    pvm.budget = db.Budget.FirstOrDefault(b=> b.Year == DateTime.Now.Year); // if you have Year property in budget model
    return View(pvm);
  }

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

1 Comment

It's what I asked in the comments...never got an answer back LOL

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.