In my attendance portal, I've set the date range "Start" to "End". In these two fields I've used date picker so the admin can search the attendances from Whatever date he selected to whatever date he wanted. But I want my start date value to be one month before the current date. I've searched it a lot but can't find better one.
Here's my View code
- class="row">class="form-group">class="col-md-3">
- @Html.DropDownList("Employee",
- (IEnumerable
)TempData["Employees"], - "All Employee",
- new { @class = "form-control", onchange = "this.form.submit();" })
class="col-md-3">- "start" autocomplete="off" name="start" class="form-control datepicker" placeholder="Start" value="@startDate" />
class="col-md-3">- "end" class="form-control datepicker" autocomplete="off" name="end" placeholder="End" value="@endDate" />
class="col-md-3">- "submit" value="Search" class="btn btn-success" />
And here's my controller code
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult All(int? Employee, int? page, string start, string end)
- {
- List
attendanceList; - if (Employee != null)
- {
- if (start != "" && end != "")
- {
- DateTime dtstart = Convert.ToDateTime(start);
- DateTime dtend = Convert.ToDateTime(end);
- attendanceList = db.Attendance.ToList().Where(x => x.Emp_Id == Employee && x.Date >= dtstart && dtend >= x.Date).ToList();
- }
- else
- {
- attendanceList = db.Attendance.ToList().Where(x => x.Emp_Id == Employee).ToList();
- }
- return View(attendanceList);
- }

Amit GuptaRank #190 · 12.8K reputationPosted Nov 16, 2020, 1:14 AM EST
Sachin SinghRank #66 · 26.2K reputationPosted Nov 16, 2020, 3:39 AM EST
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 16, 2020, 6:02 AM EST