Help with creating a view and updating an entity model
EDIT: This question is vague and will likely be of little use to anyone. I am awarding internet monies to the gentleman below whom "helped" me.
Sorry that the title is a little vague. I am still new to asp mvc & EF. Here is my issue. I have a DB somewhat like this.
Employee
- ID
- Name
- Address
EmployeeJob
- EmployeeID
- JobID
- StartDate
JobTypes
- ID
- JobName
I want a create form that will show all the Employee fields as well as a list of JobTypes for the users to be able to select. Then I will post the results back to the controller and update the DB. I have started looking into custom viewmodels, but am still unsure of exactly how to put 开发者_Python百科that together and get it to work. Thanks,
Create a specific view model that reflects the data needed by the view to display the interface. In this case, the employee information plus the collection of jobs represented by the current relations. Add to that the information required to show the jobs menu -- in this case I'd use an enumeration of SelectListItems (Value = ID, Text = JobName).
public class EmployeeViewModel
{
public Employee Employee { get; set; }
public IEnumerable<string> CurrentJobs { get; set; }
public int JobType { get; set; } // placeholder for post back value from menu
public IEnumerable<SelectListItem> JobTypes { get; set; }
}
Depending on what you are trying to do you might want a different model for the post.
public class EmployeeJobAddition
{
public Employee Employee { get; set; }
public int JobType { get; set; }
}
or (this might argue for a different collection for JobTypes in the view model)
public class EmployeeJobChange
{
public Employee Employee { get; set; }
public IEnumerable<int> CurrentJobTypes { get; set; } // returns values to keep
public IEnumerable<int> JobTypes { get; set; } // new jobs to add
}
Set up your form with input names to reflect the model being posted back, using the data supplied by the view model.
精彩评论