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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Does ModelState.IsValid works as it should?

Thread ID:

Created:

Updated:

Platform:

Replies:

126172 Sep 28,2016 01:29 PM UTC Sep 29,2016 10:21 AM UTC ASP.NET Core 1
loading
Tags: Grid
Llewelyn Jones
Asked On September 28, 2016 01:29 PM UTC

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)
    {
        db.Movies.Add(myObject.Value);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(myObject.Value);
 } 

Mani Sankar Durai [Syncfusion]
Replied On 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, 
[HomeController.cs] 
 
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. 

Regards, 
Manisankar Durai. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;