我甚至需要在下拉列表中使用onchange从View更改控制器参数变量

时间:2019-05-13 17:01:41

标签: c# ajax asp.net-mvc

我的看法基本上是3个月的下拉列表,我需要按一下其中的一个以将控制器的变量更改为按提交时的正确月份/年份

我已经尝试了多种功能,但是我对ajax既没有经验,也没有尝试过。

我基本上只是想从下拉列表中获取变量,如果你们能帮助我,我不知道该怎么做。

查看

<div class="btn-group">
            @Html.DropDownList("YearMonth", null, htmlAttributes: new { @onChange = "Funcao()" })
            @Html.ValidationMessageFor(model => model.YearMonth, "", new { @class = "text-danger" })
        </div>

控制器

public ActionResult Index(int? year, int? month)
    {
        if(!year.HasValue) {

            year = DateTime.Today.Year;
        }
        if(!month.HasValue)
        {
            month = DateTime.Today.Month;
        }

        int days = 0;

        switch (month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                days = 28;
                break;

        }

        ExtraHoursViewModel ehvm = new ExtraHoursViewModel()
        {
            EndHour = "21:00",
            StartHour = "18:00"
        };
        List<ExtraHoursViewModel> lstHours = new List<ExtraHoursViewModel>();
        lstHours.Add(ehvm);
        PresenceRecordViewModel prv = new PresenceRecordViewModel()
        {
            DaysOfMonth = new List<DayOfMonthViewModel>(),
        };

        List<Work> lstWork = db.Work.Where(c => c.Date != null).OrderBy(c => c.Date).ToList();
        for (int i = 1; i <= days; i++)
        {
            Work wk = lstWork.FirstOrDefault(c => c.Date.Value.Day == i);
            if (wk == null)
            {
                DayOfMonthViewModel dom = new DayOfMonthViewModel()
                {
                    //LastDayOfLastMonth = new DateTime(year, month, 1).AddDays(-1),
                    //FirstDayOfTheMonth = new DateTime(year, month, 1),
                    //LastDayOfTheMonth = DaysInMonth,
                    DayOfMonth = i,
                    currentYear = year.GetValueOrDefault(),
                    currentMonth = month.GetValueOrDefault(),
                    DaysInCurrentMonth = days,
                    AfternoonEndHour = "18:00",
                    AfternoonStartHour = "14:00",
                    presence = false,
                    isCheckedFP = false,
                    isCheckedSP = false,
                    ExternalService = false,
                    ExtraHoursList = lstHours,
                    MorningEndHour = "13:00",
                    MorningStartHour = "9:00",
                    //DayOfWeek = new DateTime(firstDay.Year, firstDay.Month, i).DayOfWeek,
                    //FirstDayOfNextMonth = new DateTime(year, month, 1).AddMonths(1)
                };
                prv.DaysOfMonth.Add(dom);
            }
            else 
            {
                DayOfMonthViewModel dom = new DayOfMonthViewModel()
                {
                    //LastDayOfLastMonth = new DateTime(year, month, 1).AddDays(-1),
                    //FirstDayOfTheMonth = new DateTime(year, month, 1),
                    //LastDayOfTheMonth = DaysInMonth,
                    DayOfMonth = i,
                    currentYear = year.GetValueOrDefault(),
                    currentMonth = month.GetValueOrDefault(),
                    DaysInCurrentMonth = days,
                    AfternoonEndHour = wk.SPToHour,
                    AfternoonStartHour = wk.SPFromHour,
                    presence = wk.AbsenceFP.GetValueOrDefault(false),
                    isCheckedFP = wk.AbsenceFP.GetValueOrDefault(false),
                    isCheckedSP = wk.AbsenceSP.GetValueOrDefault(false),
                    ExternalService = false,
                    ExtraHoursList = lstHours,
                    MorningEndHour = wk.FPToHour,
                    MorningStartHour = wk.FPFromHour,
                    //DayOfWeek = new DateTime(firstDay.Year, firstDay.Month, i).DayOfWeek,
                    //FirstDayOfNextMonth = new DateTime(year, month, 1).AddMonths(1)
                };
                prv.DaysOfMonth.Add(dom);
            }


        }

        var listaDeColaboradores = db.Users.Where(e => e.Customers.VAT == "503016284" && e.FullName != null).OrderBy(e => e.FullName).ToList();
        ViewData["EmployeeID"] = new SelectList(listaDeColaboradores, "Id", "FullName");
        ViewData["YearMonth"] = new SelectList(mesesApresentar(DateTime.Today.AddMonths(-1)), "Item1", "Item2", DateTime.Today.ToString("yyyyMM"));
        return View(prv);

    }

0 个答案:

没有答案