• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


25 Apr, 2025

Updated at 20 May, 2025

How to implement client-side and server-side validation for teacher updates in ASP.NET MVC?

I’m working on a school management system (ASP.NET MVC + MySQL) and need to implement update functionality for teacher records with robust validation. Here’s where I’m stuck:

Current Implementation

  1. Server-Side (API Controller)

    [HttpPut]
    [Route("api/TeacherPage/UpdateTeacher")]
    public IHttpActionResult UpdateTeacher([FromBody] Teacher teacher)
    {
        // Manual validation
        if (string.IsNullOrEmpty(teacher.TeacherFname))
            return BadRequest("Teacher name is required.");
    
        if (teacher.HireDate > DateTime.Now)
            return BadRequest("Hire date cannot be in the future.");
    
        // ... database update logic
    }
    
  2. Client-Side (Edit.cshtml)

    @model Teacher
    

Specific Problems

  1. Validation Duplication: I’m repeating checks (e.g., empty name) on both client and server. Is there a way to centralize validation rules (e.g., using [Required] annotations)?
  2. Error Handling: My AJAX call doesn’t show server validation errors (e.g., "Hire date invalid"). How can I display these errors in the form?
  3. Partial Updates: Should I use PUT (full updates) or PATCH (partial updates) for better REST compliance?

What I’ve Tried

  • Added [Required] to Teacher.cs properties, but client-side validation doesn’t trigger for AJAX.
  • Used ModelState.IsValid in the controller, but it’s always true for JSON payloads.

Environment:

  • ASP.NET MVC 5
  • jQuery 3.6
  • MySQL