Skip to content
Loading
  • +2
    Hi Varun
     
    I see you mean this following via code, all fields from the form as below;
     
    1. [Range]  
    2. public string FirstName {get;set;}  
    3.   
    4. [Range]  
    5. public string LastName {get;set;}  
    6.   
    7.   
    8. // View  
    9.   
    10.   
    11.   
    12. // Controller  
    13. public ActionResult SubmitResult() {  
    14.   
    15. if(ModelState.IsValid)  
    16. {  
    17.   
    18. // Model fields or properties here  
    19.   
    20. }  
    21.   
    22.   
    23. }  
     
    +2
  • Varun Setia
    +1
    Yes exactly, you can also add unobtrusive javascript for validation too to validate at client side.
    +1
  • Varun Setia
    +1
    But you should set proper attributes like [Required] for mandatory fields and pass that model inside controller method
    +1
  • Varun Setia
    +1
    Hi @Gcobani, is your issue resolved?
    +1
  • +1
    Hi Team
     
    I need some examples around the controller, on the model side i understand. If javascript is to put implemented i also need some pseudo code so can understand rest myself. 
    +1
  • Laxmidhar Sahoo
    +1
    hi
     
    The bellow is the example
     
    1. public class Movie  
    2. {  
    3.     public int ID { get; set; }  
    4.   
    5.     [Required(ErrorMessage = "Title is required")]  
    6.     public string Title { get; set; }  
    7.   
    8.     [Required(ErrorMessage = "Date is required")]  
    9.     public DateTime ReleaseDate { get; set; }  
    10.   
    11.     [Required(ErrorMessage = "Genre must be specified")]  
    12.     public string Genre { get; set; }  
    13.   
    14.     [Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]  
    15.     public decimal Price { get; set; }  
    16.   
    17.     [StringLength(5)]  
    18.     public string Rating { get; set; }  
    19. }  
    20.   
    21.   
    22.   
    23.  [HttpPost]  
    24.         public ActionResult Edit(Movie std)  
    25.         {  
    26.             if (ModelState.IsValid) {   
    27.               
    28.                 //write code to update movie  
    29.               
    30.                 return RedirectToAction("Index");  
    31.             }  
    32.               
    33.             return View(std);  
    34.         }  
    Thanks and regards 
    +1
  • Varun Setia
    +1
    1.   
    For client side validation 
     
    1. <script src="~/lib/jquery-validation/dist/jquery.validate.min.js">script>  
    2. <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js">script>  
     Razor Code:
     
    1. @model User
    2. <div class="form-group">    
    3.               @Html.ValidationSummary()    
    4.           div>    
    5.           <div class="form-group">    
    6.               @Html.DisplayNameFor(m => m.FirstName)    
    7.               @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })    
    8.               @Html.ValidationMessageFor(m => m.FirstName)    
    9.           div>    
    10.           <div class="form-group">    
    11.               @Html.DisplayNameFor(m => m.LastName)    
    12.               @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })    
    13.               @Html.ValidationMessageFor(m => m.LastName)    
    14.           div>  
     Class File:
     
    1. public class User  
    2. {  
    3.     [Required]  
    4.     public string FirstName { get; set; }  
    5.   
    6.     [Required]  
    7.     public string LastName { get; set; }  
    8.   
    9. }  
     Anything else required? :)
    +1
  • Amar Srivastava
    0
    So for this case you just need to validate your input modal.
     
    Now you must have to put some attributes like [Required] at the top of each property that is a required field so when you put (ModelState.IsValid) on your button click this will check with all the attributes given with the modal properties if it fills you will get the modal.isValid as true else false.
    0