ASP CORE динамически обновляет заголовки даты в viewbag - PullRequest
0 голосов
/ 03 февраля 2020

Здравствуйте. Я новичок в ядре asp, и мне было поручено сделать дневник. У меня возникают проблемы с изменением заголовков даты в таблице при нажатии кнопки на предыдущей неделе. Она должна показывать даты предыдущей и следующей недель, когда кнопки нажимаются. Заранее извиняюсь, если это действительно глупый вопрос или я собираюсь сделать это совершенно неправильно. Он работает для получения текущих недель недель, когда страница загружается, но не может переключаться между неделями.

Если больше информации не требуется, дайте мне знать .. Спасибо :)

index.cshtml
<input type="submit" method="index" value="Previous Week" class="btn btn-light" onclick="location.href = '@Url.Action("Index", "Appointment",new {mon = ViewBag.monday, tues = ViewBag.tuesday, wed = ViewBag.wednesday, thurs = ViewBag.Thursday, fri = ViewBag.friday, sat = ViewBag.saturday,sun = ViewBag.sunday})'"/>

<td width="87" align="center" <font color="#0000A0" size="1">Monday <br /> @ViewBag.monday </font></td>
            <td width="87" align="center" <font color="#0000A0" size="1">Tuesday<br>@ViewBag.tuesday</font></td>
AppointmentController
[HttpGet]
        public IActionResult Index()
        {
            ViewBag.monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday).ToString("dd-MM-yyyy");
            ViewBag.tuesday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Tuesday).ToString("dd-MM-yyyy");
            ViewBag.wednesday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Wednesday).ToString("dd-MM-yyyy");
            ViewBag.thursday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Thursday).ToString("dd-MM-yyyy");
            ViewBag.friday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Friday).ToString("dd-MM-yyyy");
            ViewBag.saturday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Saturday).ToString("dd-MM-yyyy");
            ViewBag.sunday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Sunday + 7).ToString("dd-MM-yyyy");
            return View();
        }

        [HttpPost]
        public IActionResult Index(DateTime mon, DateTime tues, DateTime wed, DateTime thurs, DateTime fri, DateTime Sat, DateTime sun)
        {
            //previous week
            ViewBag.Monday = mon.AddDays(-7);
            ViewBag.tuesday = tues.AddDays(-7);
            ViewBag.wednesday = wed.AddDays(-7);
            ViewBag.thursday = thurs.AddDays(-7);
            ViewBag.friday = fri.AddDays(-7);
            ViewBag.saturday = Sat.AddDays(-7);
            ViewBag.sunday = sun.AddDays(-7);
            return View();
        }

1 Ответ

0 голосов
/ 03 февраля 2020

Мне удалось решить эту проблему в рамках одного действия, но я думаю, что это не так, как сказал Йохан. Я опубликую свое текущее решение ниже, если оно поможет кому-нибудь в будущем!

<input type="submit" method="index" value="Previous Week" class="btn btn-light" onclick="location.href = '@Url.Action("Index", "Appointment",new {state = 1,mon = ViewBag.monday, tues = ViewBag.tuesday,
                                                                                                          wed = ViewBag.wednesday, thurs = ViewBag.Thursday, fri = ViewBag.friday, sat = ViewBag.saturday,
                                                                                                          sun = ViewBag.sunday})'"/>

                <input type="submit" value="Current Week" class="btn btn-light" onclick="location.href = '@Url.Action("Index", "Appointment",new {state = 0,mon = ViewBag.monday, tues = ViewBag.tuesday,
                                                                                                          wed = ViewBag.wednesday, thurs = ViewBag.Thursday, fri = ViewBag.friday, sat = ViewBag.saturday,
                                                                                                          sun = ViewBag.sunday})'"/>

                <input type="submit" value="Next Week" class="btn btn-light" onclick="location.href = '@Url.Action("Index", "Appointment",new {state = 2,mon = ViewBag.monday, tues = ViewBag.tuesday,
                                                                                                          wed = ViewBag.wednesday, thurs = ViewBag.Thursday, fri = ViewBag.friday, sat = ViewBag.saturday,
                                                                                                          sun = ViewBag.sunday})'"/>
public IActionResult Index(int state, string mon, string tues, string wed, string thurs, string fri, string Sat, string sun)
        {
            //var mon
            if (state <= 0)
            {
                ViewBag.monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday).ToString("dd-MM-yyyy");
                ViewBag.tuesday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Tuesday).ToString("dd-MM-yyyy");
                ViewBag.wednesday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Wednesday).ToString("dd-MM-yyyy");
                ViewBag.thursday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Thursday).ToString("dd-MM-yyyy");
                ViewBag.friday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Friday).ToString("dd-MM-yyyy");
                ViewBag.saturday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Saturday).ToString("dd-MM-yyyy");
                ViewBag.sunday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Sunday + 7).ToString("dd-MM-yyyy");
                return View();
            }
            else if(state == 1){
                var pmon = Convert.ToDateTime(mon).AddDays(-7);
                var ptues = Convert.ToDateTime(tues).AddDays(-7);
                var pwed = Convert.ToDateTime(wed).AddDays(-7);
                var pthur = Convert.ToDateTime(thurs).AddDays(-7);
                var pfri = Convert.ToDateTime(fri).AddDays(-7);
                var psat = Convert.ToDateTime(Sat).AddDays(-7);
                var psun = Convert.ToDateTime(sun).AddDays(-7);
                ViewBag.Monday = pmon.ToString("dd-MM-yyyy"); 
                ViewBag.tuesday = ptues.ToString("dd-MM-yyyy");
                ViewBag.wednesday = pwed.ToString("dd-MM-yyyy");
                ViewBag.thursday = pthur.ToString("dd-MM-yyyy");
                ViewBag.friday = pfri.ToString("dd-MM-yyyy");
                ViewBag.saturday = psat.ToString("dd-MM-yyyy");
                ViewBag.sunday = psun.ToString("dd-MM-yyyy");
                return View();
            }
            else if (state == 2)
            {
                var pmon = Convert.ToDateTime(mon).AddDays(7);
                var ptues = Convert.ToDateTime(tues).AddDays(7);
                var pwed = Convert.ToDateTime(wed).AddDays(7);
                var pthur = Convert.ToDateTime(thurs).AddDays(7);
                var pfri = Convert.ToDateTime(fri).AddDays(7);
                var psat = Convert.ToDateTime(Sat).AddDays(7);
                var psun = Convert.ToDateTime(sun).AddDays(7);
                ViewBag.Monday = pmon.ToString("dd-MM-yyyy");
                ViewBag.tuesday = ptues.ToString("dd-MM-yyyy");
                ViewBag.wednesday = pwed.ToString("dd-MM-yyyy");
                ViewBag.thursday = pthur.ToString("dd-MM-yyyy");
                ViewBag.friday = pfri.ToString("dd-MM-yyyy");
                ViewBag.saturday = psat.ToString("dd-MM-yyyy");
                ViewBag.sunday = psun.ToString("dd-MM-yyyy");
                return View();
            }
            else
            {
                return View("NotFound");
            }
...