将View生成的变量保存到我的数据库

时间:2019-04-24 11:20:08

标签: c# asp.net-mvc datetime variables razor

我正在创建MVC应用程序,并且不想使用外部库。我需要用相关日期填充特定的一周,并且这将每周更改一次,然后我要将包含相关人员和该日期的项目信息的日期保存到数据库中。我相信我需要给相关的表格标签加上一个ID,但是我不知道该怎么做。有人可以指出我正确的方向吗?

我已经创建了模型并自动生成了控制器和视图,我仍在编辑视图并意识到我有此问题。我已经找到了如何填充特定一周的日期,但是不知道如何在MVC中实现它。

        var currentWeek = Convert.ToInt32(Console.ReadLine());

        var newWeek = GetDateFromWeekNumberAndDayOfWeek(currentWeek);

        for (int i = 0; i < 7; i++)
        {
            var weekDays = newWeek.AddDays(i);
            if (weekDays.DayOfWeek != DayOfWeek.Sunday && weekDays.DayOfWeek != DayOfWeek.Saturday)
            {
                Console.WriteLine("Day " + (i + 1) + " " + weekDays.ToString("dddd, dd MMMM yyyy"));
            }
        }
        Console.ReadLine();
    }
    static DateTime GetDateFromWeekNumberAndDayOfWeek(int weekNumber)
    {
        DateTime jan1 = new DateTime(DateTime.Now.Year, 1, 1);
        int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;
        // Use first Thursday in January to get first week of the year as it will never be in Week 52/53
        DateTime firstThursday = jan1.AddDays(daysOffset);

        var cal = CultureInfo.CurrentCulture.Calendar;
        int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

        var weekNum = weekNumber;
        // As we're adding days to a date in Week 1, we need to subtract 1 in order to get the right date for week #1
        if (firstWeek <= 1)
        {
            weekNum -= 1;
        }
        // Using the first Thursday as starting week ensures that we are starting in the right year then we add number of weeks multiplied with days
        var result = firstThursday.AddDays(weekNum * 7);
        // Subtract 3 days from Thursday to get Monday, which is the first weekday in ISO8601
        //addition use -4 to get Sunday as first day
        return result.AddDays(-4);
    }

0 个答案:

没有答案