0

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

  <div class="row">
        <div class="form-group">
            <div class="col-md-3">
                @Html.DropDownList("Employee",
                        (IEnumerable<SelectListItem>)TempData["Employees"],
                        "All Employee",
                        new { @class = "form-control", onchange = "this.form.submit();" })
            </div>
            <div class="col-md-3">
                <input id="start" autocomplete="off" name="start" class="form-control datepicker" placeholder="Start" value="@startDate" />

            </div>
                <div class="col-md-3">
                    <input id="end" class="form-control datepicker" autocomplete="off" name="end" placeholder="End" value="@endDate" />
                </div>
                <div class="col-md-3">
                    <input type="submit" value="Search" class="btn btn-success" />
                </div>

            </div>
    </div>

And here's my controller code

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult All(int? Employee, int? page, string start, string end)
        {
            List<Attendance> 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);
        }


HamzaShah
  • 43
  • 6
  • If it *must* be exactly 1 month, then why do you have 2 date pickers? Just have one and calculate the other date. However, what exactly is the issue? "searched *it*"? What's "it" ? How to set a date in a date picker? How to get a date from a date picker? How to [add months](https://stackoverflow.com/questions/2706125/javascript-function-to-add-x-months-to-a-date)? How to search in SO? – freedomn-m Nov 16 '20 at 11:42
  • No it not must be one month, i just want to show the attendances on the page from today's date to before today's date month by default. – HamzaShah Nov 16 '20 at 11:49
  • So what's the issue? Looks like you just need to set `@startDate` in your view. How's this being set at the moment? – freedomn-m Nov 16 '20 at 12:19
  • yes i know it will happen in view using jquery. but i don't know hot to implement it – HamzaShah Nov 16 '20 at 12:31
  • `@startDate` isn't jquery - that's a variable in your Razor view - how are you setting it now? – freedomn-m Nov 16 '20 at 12:32

0 Answers0