Reputation: 1389
I create the Scaffold of Department and Designation for controllers.
I have the following project structure:
with the following database structure:
I want to create one view page for these scenario. Like this:
Insert Name of Form (TextBox) + Department Name (Dropdown list box) + Designation Name (Dropdown list box)
Department.cs
namespace mvcAppraisalSystem.Models
{
public class Department
{
[Key]
public int deptID { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
public class CompanyDBContext : DbContext
{
public DbSet<Department> Departments { get; set; }
}
}
Designation.cs
namespace mvcAppraisalSystem.Models
{
public class Designation
{
[Key]
public int desgID { get; set; }
public string desgName { get; set; }
public string description { get; set; }
}
public class RoleDBContext : DbContext
{
public DbSet<Designation> Designations { get; set; }
}
}
Upvotes: 1
Views: 178
Reputation: 1963
There are a lot of ways to do this. You could create a view model, which would be a specific model for this view. The view model would contain lists of both departments and designations as well as the id of the selected department, designation, and the field.
Then you use a strongly typed view to the view model.
Example of a possible view model:
public class MyScenarioForm
{
[Key]
public string FormName { get; set; }
public int SelectedDesgId {get; set;}
public int SelectedDeptId { get; set; }
public IEnumerable<Designation> Designations { get; set; }
public IEnumerable<Department> Departments { get; set; }
// ... constructor or method that creates initial instance with Designations and Departments populated
}
Another way to handle the dropdowns would be to use partial views for those dropdowns. This way the view model for this form would not need to have lists of designations and departments. This would also be good if you are going to reuse these dropdowns elswhere then you can just reuse these partial views.
Upvotes: 1