I am new to MVC
I have an Employee POCO like this
[PetaPoco.TableName("tblEmployee")]
[PetaPoco.PrimaryKey("EmployeeId")]
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public int DepartmentId { get; set; }
}
Department Id is actually a foreign key coming from Table tblDepartment. So I want to limit the value of DepartmentId in creating new Employee as the values existing in table tblDepartment(column : Id ).How to do this?
Existing code in Create View @Html.EditorFor(model => model.DepartmentId)
Usually reference integrity is managed at a database level.
Make the relationship on your database between Employees and Departments to enforce reference integrity, so if you try to save an Employee whose department id don't exist on Department table it should return an integrity error.
Capture this error on your saving controller and return it to user.
Also, it will help if you allow your users only to load data from a limited list loaded with existing data on the database, as it is suggested in comments.
However this could not be enough, as a valid value could be deleted from database between the moment it was posted to the list in your form and the moment the form is sent to the controller to save data.