Custom Validation MVC 5

十年热恋 提交于 2020-01-06 21:42:11

问题


Ok i have this Edit form, what i need is some kind of custom validation for when i enter ReturnDate it Shouldn't be lower then the date of IssueDeadLineDate value. Can i do with Some kind of custom validation or what do i need ?

Edit View Picture

  namespace Library.Models
 {
  public class BookStatusView
  {
    public int Id { get; set; }
    public string UserId { get; set; }
    public DateTime IssueDeadlineDate { get; set; }
    public DateTime ReturnDeadlineDate { get; set; }

    public DateTime? ReturnDate { get; set; }
    public int BookId { get; set; }
    public string AspNetUser { get; set; }
    public string BookName { get; set; }


    public IEnumerable<SelectListItem> qwerty { get; set; }

    public string Email { get; set; }
    public string FirstName { get; set; }

This is my BookStatusView model where i want to implement custom validation. If u need any other code feel free to ask i will paste it ass soon as i see it.


回答1:


if you are looking to do server side validation you can do a simple comparison

if(ReturnDate < IssueDeadlineDate) in the controller

if you are wanting client side validation you will need to compare the model values in a similar way but in javascript

You could also try using a custom ValidationAttribute

http://dotnetmentors.com/mvc/how-to-do-custom-validation-using-validationattribute-of-aspnet-mvc.aspx



来源:https://stackoverflow.com/questions/43215165/custom-validation-mvc-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!