We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Does ModelState.IsValid works as it should?

I have the following controller method. However, my POCO's Model class is now found in myObject.Value property. Does ModelState.IsValid works as it should, given that it is now buried in another class? Could you suggest a similar technique if ModelState.IsValid ceases to work.

public ActionResult Create([FromBody] CRUDModel<MyType> myObject) 
   if (ModelState.IsValid)
        return RedirectToAction("Index");

    return View(myObject.Value);

1 Reply

MS Mani Sankar Durai Syncfusion Team September 29, 2016 10:21 AM UTC

Hi Llewelyn, 

Thanks for contacting Syncfusion support. 
We have analyzed your query and we have prepared a sample based on your requirement. 
Please find the sample in the below attached link, 

In this sample, ModelState.isValid has been worked based on its validation we passed for the particular column in grid. For example when CustomerID column should not exceeded the stringLength of 5. If it exceeded ModelState.isValid will be false else it will be true. 

Please refer the below code example, 
public class Orders 
            [Range(0, int.MaxValue, ErrorMessage = "OrderID must be greater than 0.")] 
            public long OrderID { get; set; } 
            [StringLength(5, ErrorMessage = "CustomerID must be 5 characters.")] 
            public string CustomerID { get; set; } 
            [StringLength(15, ErrorMessage = "ShipName must be 15 characters.")] 
            public string ShipCity { get; set; } 
public ActionResult Update([FromBody]CRUDModel<Orders> myObject) 
            if (ModelState.IsValid) 
                var ord = myObject.Value; 
                return Json(myObject.Value); 
            if (!ModelState.IsValid) 
                return new StatusCodeResult(0); 
            return View(); 

Also please refer the below screenshot about ModelState.isValid is working 
Screenshot1: When ModelState.isValid is true. 
Screenshot1: When ModelState.isValid is false. 

Also we have displayed the error message in alert box using ActionFailure event in grid. ActionFailure event triggered for every grid action server failure event

Please let us know if you need further assistance. 

Manisankar Durai. 

Live Chat Icon For mobile
Up arrow icon