根据年份和周数显示数据

时间:2014-10-02 07:38:09

标签: c# asp.net asp.net-mvc asp.net-mvc-4 datetime

我正在开发一个应用程序,管理员在其中键入Datetime作为搜索字词以匹配数据库中的戳记日期。

我需要更改它,以便搜索条件可以基于年份和周数。该场景是用户在GetWeekStamp视图中输入年份和周数,在他们发布重定向到Stampings视图后,他们根据输入的年份和周数显示数据。

模型1:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Aviato.Models
{
    public partial class Stamping
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        public int UserId { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        public DateTime Timestamp { get; set; }

        [Required]
        [StringLength(3)]
        public string StampingType { get; set; }

        public virtual User User { get; set; }
    }
}

模型2:

using System;
using System.Collections.Generic;
using Aviato.Models;

namespace Aviato.ViewModel
{
    public class StampingModel
    {
    public List<Stamping> Stampings { get; set; }
    public DateTime Timestamp { get; set; }
    }
}

查看(GetWeekStamp):

@model Aviato.Models.Stamping
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <h1>Ange Datum</h1>
    <p>(ÅÅÅÅ-MM--DD)</p>
    @Html.EditorFor(model => model.Timestamp, new { htmlAttributes = new { @class = "form-control" } })
    <br/>
    <br />
    <input type="submit" value="Välj" class="btn btn-default">
    <br />
    <br />
    <div>
        @Html.ActionLink("Tillbaka", "Index")
    </div>
}

查看(冲压)

@model Aviato.ViewModel.StampingModel
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <h2>Instämplingar</h2>

    <p>Du har valt: @Model.Timestamp.Year-@Model.Timestamp.Month-@Model.Timestamp.Day</p>

    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Stampings[0].Timestamp)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Stampings[0].StampingType)
            </th>
        </tr>
        @foreach (var item in Model.Stampings)
        {
            <tr>
                <td>
                    @Html.DisplayFor(model => item.Timestamp)
                </td>
                <td>
                    @Html.DisplayFor(model => item.StampingType)
                </td>
            </tr>
        }
    </table>
    <p>
        @Html.ActionLink("Tillbaka", "GetWeekStamp")
    </p>
}

控制器:

public ActionResult GetWeekStamp()
{
    return View();
}

[HttpPost]
public ActionResult GetWeekStamp(Stamping model)
{
    return RedirectToAction("Stampings", new { model.Timestamp });
}

public ActionResult Stampings(Stamping model)
{            
    var startTime = model.Timestamp;
    var endTime = model.Timestamp.AddDays(1);
    var userId = (int)Session["userId"];

    var stampings = _db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp <= endTime)
        .Where(s => s.UserId == userId).ToList();

    var stampingModel = new StampingModel();

    stampingModel.Stampings = stampings;
    stampingModel.Timestamp = model.Timestamp;

    return View(stampingModel);
}

我在Stack中找到了这个Converter类,但我不知道该怎么做...

类别:

public class DateConverter
{
    public static DateTime FirstDateOfWeek(int year, int weekOfYear)
    {
        var jan1 = new DateTime(year, 1, 1);
        var daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;
        var firstThursday = jan1.AddDays(daysOffset);
        var cal = CultureInfo.CurrentCulture.Calendar;
        var firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
        var weekNum = weekOfYear;

        if (firstWeek <= 1)
        {
            weekNum -= 1;
        }

        var result = firstThursday.AddDays(weekNum * 7);

        return result.AddDays(-3);
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用以下扩展方法获取周数:

public static int GetWeekNumber(this DateTime dateTime)
{
    CultureInfo currentCulture = CultureInfo.CurrentCulture;

    return currentCulture.Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

从DateTime获得一年只需使用:

var datetime = DateTime.Now; //i'm using Now, but you can use the one from Timestamp ...
var year = datetime.Year;

答案 1 :(得分:1)

您需要为GetWeekStamp视图

创建模型
using System;
using System.Collections.Generic;
using Aviato.Models;

namespace Aviato.ViewModel
{
    public class GetWeekStampModel
    {
        public int Year { get; set; }
        public int WeekNo { get; set; }
    }
}

然后将GetWeekStamp视图代码更改为此,以便用户输入年份和周数

@model Aviato.ViewModel.GetWeekStampModel
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <h1>Year</h1>
    @Html.EditorFor(model => model.Year, new { htmlAttributes = new { @class = "form-control" } })
    <br />
    <br />
    <h1>Week No</h1>
    @Html.EditorFor(model => model.WeekNo, new { htmlAttributes = new { @class = "form-control" } })
    <br />
    <br />
    <input type="submit" value="Välj" class="btn btn-default">
    <br />
    <br />
    <div>
        @Html.ActionLink("Tillbaka", "Index")
    </div>
}

然后在GetWeekStamp控制器帖子操作方法中,将输入的年份和周数传递给Stampings视图

[HttpPost]
public ActionResult GetWeekStamp(GetWeekStampModel model)
{
    return RedirectToAction("Stampings", new { year = model.Year, weekNo = model.WeekNo });
}

应移除Timestamp类中的StampingModel属性,并添加两个新属性YearWeekNo

using System;
using System.Collections.Generic;
using Aviato.Models;

namespace Aviato.ViewModel
{
    public class StampingModel
    {
        public List<Stamping> Stampings { get; set; }
        public int Year { get; set; }
        public int WeekNo { get; set; }
    }
}

Stampings控制器获取操作方法中,您可以使用上面的year方法根据weekNoFirstDateOfWeek计算一周的第一个日期,然后将结果用作startTime endTime并将startTime设置为public ActionResult Stampings(int year, int weekNo) { var startTime = DateConverter.FirstDateOfWeek(year, weekNo); var endTime = startTime.AddDays(6); var userId = (int)Session["userId"]; var stampings = _db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp <= endTime) .Where(s => s.UserId == userId).ToList(); var stampingModel = new StampingModel(); stampingModel.Stampings = stampings; stampingModel.Year = year; stampingModel.WeekNo = weekNo; return View(stampingModel); }

后六天
Stampings

最后在<p>Du har valt: @Model.Timestamp.Year-@Model.Timestamp.Month-@Model.Timestamp.Day</p> 视图

中更改此部分
<p>Du har valt: @Model.Year-@Model.WeekNo</p>

到这个

{{1}}