I am trying to build a employee attendance sheet.
Here is my AttendanceModel
public class AttendanceModel
{
//Primary Key and auto-generated field for this model
public int AttendanceID { get; set; }
public DateTime AttendanceDate { get; set; }
//Present/Absent status
public bool Status { get; set;}
//Primary key of Employee Model
public int EmployeeID { get; set; }
//Reference to Employee Model
public Employee Employee { get; set; }
}
Now I want this model to make a View
containing employeeId(Hidden field)
, Name of Employee and a checkbox for attendance.
My problem is (Edited on 3rd day)
- Should i have to use a different ViewModel for this view
- How to insert the information from
view
to the entity and ultimately to databse as it require more than one row to be inserted in the database (i.e Batch Processing) - I am also not getting how to retrieve the information from the view to do processing
Any suggestion is appreciable. :)
Thanks in Advance !!!
*EDIT (What i have tried) *
1. I have created another class AttendanceViewModel
containing all the field of AttendanceModel
except the reference to Employee
model class. then in view
I render it. In the controller the argument is IEnumerable<AttendanceViewModel>
and from it populate the Attendance Model. Here is the code.
public ActionResult Index()
{
var EmployeeAttendance = from s in new EmployeeDBContext().Employees
select new AttendanceViewModel
{
EmployeeID = s.EmployeeID
};
return View(EmployeeAttendance.ToList());
}
主计长为节省记录而采取的行动
[HttpPost]
public ActionResult SaveAttendance(IEnumerable<AttendanceViewModel> attendancess) // argument of SaveAttendance is always `null`
{
if (ModelState.IsValid)
{
var _context = new EmployeeDBContext();
foreach (var a in attendancess)
{
var newAttendance = new AttendanceModel
{
EmployeeID = a.EmployeeID,
AttendanceStatus = a.Status
};
_context.Attendance.Add(newAttendance);
}
_context.SaveChanges();
}
//Rest of code
但是,其并非工作理由总是null
: