简体   繁体   English

设置用户输入数据的时间限制的最佳方法是什么? (asp.net mvc)

[英]What is the best way to set time boundries for user to input data? (asp.net mvc)

I'm actually wrapping my head around the problem of time. 我实际上是在解决时间问题。 I wish to allow the user to input data between Monday-Wednesday (Monday 00:00 to Wednesday 23:59) each week just once AND create a unique id that would represent each week. 我希望允许用户每周一次在星期一至星期三(星期一00:00到星期三23:59)之间输入数据,并创建一个代表每周的唯一ID。

I need this id later, so the user can't insert another piece of the same data until next week opening. 稍后我需要此ID,以便用户在下周开放之前无法插入其他相同数据。

At first, I went for DateTime.Today but then realised that it doesn't prevent the user from inserting another piece of data the next (or the other) day. 起初,我去了DateTime.Today,但是后来意识到,这并不能阻止用户在第二天(或另一天)插入另一条数据。

Now I'm thinking of creating an action filter that would block out the user if he has already signed in data for the week and take it on the next Monday. 现在,我正在考虑创建一个动作过滤器,如果用户已经登录了本周的数据并在下一个星期一使用它,它将阻止该用户。 I'd need a function that resets the flag on Monday 00:00 and raise flag on Thursday 00:00 (if user hasn't signed the data in) which atm seems a lot of time-control work (that's why I went for unique id 1st) and setting on/off the action filter (which I don't know if is really possible or how to do it). 我需要一个在星期一00:00重置标志并在星期四00:00升高标志(如果用户尚未签名数据)的功能,这似乎是很多时间控制工作的atm(这就是为什么我去了唯一ID 1st)并设置动作过滤器的开/关(我不知道这是否真的可行或如何执行)。

I think that this is a bad road to follow meaning there could be another way to achieve my goal. 我认为这是一条艰难的路,意味着可能会有另一种方式来实现我的目标。 Or there might even be some kind of solution already. 甚至可能已经有某种解决方案。 That's why I ask you here, hoping for inspiration. 这就是为什么我在这里问你,希望能得到启发。

Thanks! 谢谢!

EDIT: 编辑:

All of the above concerns raport_id as unique id. 以上所有问题都将raport_id作为唯一ID。

db classes: db类:

-raport -拉波特

public class Raport
    {
[DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Key]
        public string raport_id { get; set; } //id raportu
        public string userID { get; set; }

        public DateTime creation_time { get; set; }

        public int isMinus { get; set; }

        public DateTime last_modyfication { get; set; }


        public virtual ICollection<ThrashType> ThrashType { get; set; } 


        public virtual UserFrontInfo UserFrontInfo { get; set; }       
    }

-thrash type 跳动类型

public class ThrashType
{

    [Key] 
    public int IdTrash { get; set; }
    public string raport_id { get; set; } 
    public string ClassName { get; set; } 

    public bool isNegative { get; set; }
    public int Quantity { get; set; } 

    public string Information { get; set; } 

    public virtual Raport Raport { get; set; }   

}

creating raport_id: 创建raport_id:

            var datePart = DateTime.Today.ToString().Replace(".", "").Replace(":","").Replace(" ","");
            var namePart = User.Identity.Name.ToString();

            var nameShort = namePart.Remove((namePart.Length)-13, 12); 

            newRaport.raport_id = datePart + nameShort;

Some concrete classes that represent the database would really help illustrate your question. 某些代表数据库的具体类确实可以帮助说明您的问题。

How you implement the ultimate solution, be it an action filter, a check in the action method, or anywhere else does not really matter. 最终解决方案的实现方式,无论是动作过滤器,动作方法的检查,还是其他任何地方,都没有关系。

When users can enter data once per week, then simply store the week number with the data, and when they try to enter more data, check whether data for the given week number already exists. 当用户每周可以输入一次数据时,只需将星期几与数据一起存储,当他们尝试输入更多数据时,请检查给定星期数的数据是否已经存在。

See Get the correct week number of a given date for an implementation of obtaining week numbers for given dates. 有关获取给定日期的星期数的实现,请参阅获取给定日期的正确星期数

It seems that you are twisting the problem for yourself. 看来您正在为自己扭曲问题。 From your text, I understand this: 从您的文字中,我理解:

You want to prevent user from adding a row to your table more than once a week. 您要防止用户每周多次在表中添加一行。

You can check the week by: 您可以通过以下方式查看一周

cal.GetWeekOfYear(date1, dfi.CalendarWeekRule,dfi.FirstDayOfWeek)

It is also easy to check your new row to see if there is another row in that week. 检查您的新行以查看该周是否还有另一行也很容易。

ie: if you use sqlserver (t-sql) you have something like this 即:如果您使用sqlserver(t-sql),则有类似这样的内容

SELECT DATEPART(wk, GETDATE())

This is very important: 这个非常重要:

Don't set a flag for something like this. 不要为这样的事情设置标志。 Just check your table and see if a row exists for that person in that week and you are good to go. 只要检查您的表,看看在那个星期该人是否存在一行,您就该走了。

If your concern is the performance, by adding a flag you will do the same process to call database, believe me this is not a scenario that you need to implement a job and... 如果您关心的是性能,则通过添加标志,您将执行相同的过程来调用数据库,相信我,这不是您需要实现作业的情况,并且...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM