2015-03-13 83 views
0

在Visual Studio中工作,并在我的控制器类中使用名为GroupController的当前代码。在群组页面上,我可以创建一个新群组。然后,用户将输入groupName,groupAssignment,groupLocation和GroupDateTime的字符串值。到目前为止,我所提供的代码在下面提供,但一直在努力寻找日期的工作比较。我想确保输入日期大于当前日期。如果不是,则不允许用户创建组。有什么建议么?谢谢日期输入验证asp.net

public ActionResult Create([Bind(Include = "GroupID,GroupName,GroupAssignment,GroupLocation,GroupDateTime")] Group group) 
    { 
     var currentUser = manager.FindById(User.Identity.GetUserId()); 

     try 
     { 
      group.User = manager.FindById(User.Identity.GetUserId()); 
      db.Groups.Add(group); 
      db.SaveChanges(); 
      return RedirectToAction("Index");   
     } 
     catch (Exception /* dex */) 
     { 
      if(group.User == null) 
      { 
       ModelState.AddModelError("", "No user logged in"); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Unhandled Error"); 
      } 
     } 
     return View(group); 
    } 
+0

你有没有试过将'GroupDateTime'解析为'DateTime',并将它与'DateTime.Now'或其他东西比较? – 2015-03-13 12:53:07

+0

是的,我尝试过使用if(group.GroupDateTime KimCheeFatChoyProgrammer 2015-03-13 12:58:22

+0

从你的问题听起来像'group.GroupDateTime'是一个'string'(这是否正确?),所以比较不起作用。您需要先将字符串转换为“DateTime”。你至少应该这样做,以确定你的工作是否会有效的日期和时间。 – MotoSV 2015-03-13 13:05:18

回答

1

我建议您在模型(组)上使用自定义验证。你可以在GroupDateTime财产使用CustomAttribute,如下图所示:

[AttributeUsage(AttributeTargets.Property)] 
public class FutureDateValidation : ValidationAttribute { 
    protected override ValidationResult IsValid(object value, ValidationContext validationContext) { 
     DateTime inputDate = (DateTime)value; 

     if (inputDate > DateTime.Now) { 
      return ValidationResult.Success; 
     } else { 
      return new ValidationResult("Please, provide a date greather than today.", new string[] { validationContext.MemberName }); 
     } 
    } 
} 


public class Group { 
    [FutureDateValidation()] 
    [DataType(DataType.Date, ErrorMessage = "Please provide a valid date.")] 
    public DateTime GroupDateTime { get; set; } 
} 

为了garantee数据类型的验证,你应该使用属性[数据类型(DataType.Date)。您可以使用客户端脚本来放置遮罩,例如使用jquery-ui。

此外,您可以用控制器或操作中的[Authorize]属性和基本控制器上的“Unhandled Error”重写HandleException来替换“no user logged in”。

我强烈建议您使用声明式验证。 http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

+0

啊!当然,这工作非常好。我从来不知道如何实现自定义验证,所以非常感谢。我也将在[授权]属性中工作。 – KimCheeFatChoyProgrammer 2015-03-13 13:17:45

+0

关于ASP.Net MVC安全的伟大视频: http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m7-security&mode=live&clip=0&course=mvc4-building – 2015-03-13 13:22:28

1

try声明之前,你可以使用DateTime.TryParse。如果返回false,则将错误添加到ModelState。如果它返回true,那么你有一个有效的日期和时间,然后你可以与DateTime.Now比较。

所以:

DateTime temp; 
if(!DateTime.TryParse(group.GroupDateTime, out temp)) 
{ 
    this.ModelState.AddError(....); 
    return /* add action result here */ 
} 

if(temp < DateTime.Now) 
{ 
    this.ModelState.AddError(....) 
    return /* add action result here */ 
} 

...everything else... 

更新#1:你应该考虑改变Group性能更加强类型的,所以更改GroupDateTimeDateTime。这也将允许您使用内置的验证属性,如MaxLengthAttribute,RequiredAttribute等。

+0

我试图在这里实现这一点,并收到无法将系统时间转换为字符串的错误。有什么想法吗? – KimCheeFatChoyProgrammer 2015-03-13 13:12:26

+0

您确定传入GroupDateTime的字符串是否为有效的DateTime?如果没有,那么在你的方法中添加验证就会捕获这个,你应该在客户端添加额外的验证来确保输入正确的DateTime。尝试更改UI控件以仅允许日期/时间。 – MotoSV 2015-03-13 13:15:46

+0

我会继续努力,找到多种方式去追求。那是我遇到的第一个问题。验证照顾了这一点。谢谢@MotoSV – KimCheeFatChoyProgrammer 2015-03-13 13:19:16