我将在明天为我正在工作的会议应用程序编写一个自定义日期验证课程,该工作将验证给定的开始或结束日期是否小于当前日期,或者B)开始日期大于会议结束日期(反之亦然)。
我认为这可能是一个相当普遍的要求。任何人都可以指向我可能帮助我解决这个问题的博客文章的方向吗?
我正在使用.net 3.5,所以我不能使用.NET 4中内置的新模型验证器api。我正在研究的项目是MVC 2.
更新:我正在编写的类需要扩展System.ComponentModel.DataAnnotations命名空间。在.NET 4中,有一个可以实现的IValidateObject接口,这使得这种事情变得绝对轻而易举,但遗憾的是我不能使用.Net 4.我如何在.Net 3.5中做同样的事情?< / p>
答案 0 :(得分:21)
public sealed class DateStartAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
DateTime dateStart = (DateTime)value;
// Meeting must start in the future time.
return (dateStart > DateTime.Now);
}
}
public sealed class DateEndAttribute : ValidationAttribute
{
public string DateStartProperty { get; set; }
public override bool IsValid(object value)
{
// Get Value of the DateStart property
string dateStartString = HttpContext.Current.Request[DateStartProperty];
DateTime dateEnd = (DateTime)value;
DateTime dateStart = DateTime.Parse(dateStartString);
// Meeting start time must be before the end time
return dateStart < dateEnd;
}
}
并在您的视图模型中:
[DateStart]
public DateTime StartDate{ get; set; }
[DateEnd(DateStartProperty="StartDate")]
public DateTime EndDate{ get; set; }
在您的操作中,只需检查ModelState.IsValid即可。那你在追求什么?
答案 1 :(得分:4)
我知道这篇文章比较老,但是,我发现这个解决方案要好得多。
如果对象在视图模型中是一个前缀,则此帖子中接受的解决方案将无效。
即。线条
// Get Value of the DateStart property
string dateStartString = HttpContext.Current.Request[DateStartProperty];
可以在这里找到更好的解决方案: http://www.a2zdotnet.com/View.aspx?Id=182
答案 2 :(得分:0)
我认为应该这样做:
public boolean MeetingIsValid( DateTime start, DateTime end )
{
if( start < DateTime.Now || end < DateTime.Now )
return false;
return start > end || end < start;
}
答案 3 :(得分:-1)
public ActionResult Index()
{
return View("department1");
}
public ActionResult Savedata(Vmdepartment vm)
{
Hrcontext context = new Hrcontext();
context.deptentity.Add(vm.dept);
context.SaveChanges();
return View("department1", vm);
}
public ActionResult Index1(Vmdepartment vm)
{
vm.liDepartment = new List<department>();
return View("departmentview",vm);
}
public ActionResult search(Vmdepartment vm) {
var n = vm.dept.name;
vm.liDepartment = new List<department>();
Hrcontext context = new Hrcontext();
vm.liDepartment = context.deptentity.Where(a => a.name == n).ToList();
return View("departmentview",vm);
}
}
答案 4 :(得分:-1)
public ActionResult Index(vmemployee vme)
{
emplyee emp = new emplyee();
vme.cities = new List<city>();
vme.countries = new List<country>();
vme.departments = new List<department>();
Hrcontext context = new Hrcontext();
vme.countries = context.cntentity.ToList();
vme.cities = context.cityentity.ToList();
vme.departments = context.deptentity.ToList();
return View("employeelist", vme);
}
public ActionResult Index1(vmemployee vm)
{
vm.liemp = new List<emplyee>();
return View("Searchemployee", vm);
}
[submit(Name = "sav")]
public ActionResult save(vmemployee vme)
{
vme.cities = new List<city>();
vme.countries = new List<country>();
vme.departments = new List<department>();
Hrcontext context = new Hrcontext();
vme.countries = context.cntentity.ToList();
vme.cities = context.cityentity.ToList();
vme.departments = context.deptentity.ToList();
vme.emp.Imagefile.SaveAs(@"C:\inetpub\wwwroot\hrsestem\hrsestem\images\" + vme.emp.Imagefile.FileName);
vme.emp.Imagepath= (@"C:\inetpub\wwwroot\hrsestem\hrsestem\images\" + vme.emp.Imagefile.FileName);
if (ModelState.IsValid == true)
{
context.empentity.Add(vme.emp);
context.SaveChanges();
}
return View("employeelist", vme);
}