Skết thúc method is called and the ModelState.IsValid will return true.Now to my question when will ModelState.IsValid return false ?I have sầu tested this and if you for example add thisModelState.AddModelError("", "One or more error were found");before ModelState.IsValid then ModelState.IsValid will return false.The reason for checking ModelState.IsValid is two as I believe sầu.1. If a Business rule is not valid you must use code lượt thích thisModelState.AddModelError("", "Some business rule is not valid");which will cause ModelState.IsValid to lớn return false and the user can change the entered data2. If the user have disable javascript to be able to run on client side.Give sầu me a bình luận about this if I have sầu understood this correct. public ActionResult Send(MessageModel model) if (ModelState.IsValid) return RedirectToAction("Thank you"); return View(model); //Tony
JohnLockeContributor
7387 Points
1984 Posts
Re: About ModelState.IsValid
Jul 01, 2014 08:49 AM|JohnLocke|LINK
TojoNow to lớn my question when will ModelState.IsValid return false ?If your data does not match the guidelines of your defined Model, it will return false.
Example:
public ActionResult Edit(Product formData) if (ModelState.IsValid) // vì stuff with the data return View(model);In this example, if any of the data you submitted ("formData") does not fit the type of properties or their annotations of the given Mã Sản Phẩm type, the ModelState will return false, và your data will be returned back lớn the view with errors lớn provide the user with details of what the form expects.Xem thêm: Đánh Giá Xe Ford Focus 2017, Mua Bán Xe Ford Focus 2017 Cũ Chính Chủ Giá Rẻ
Rion William...All-Star
114593 Points
18503 Posts
MVP
Re: About ModelState.IsValid
Jul 01, năm trước 09:18 AM|Rion Williams|LINK
The ModelState.IsValid property will return false if the server-side validation on your Model that was posted (via an HttpPost or a form submission) if any of the properties on your particular Model did not meet their required validation.
For example, if you had the following Mã Sản Phẩm :
public class TestModel public string ExampleProperty get; set; public string AnotherProperty get; set; public TestModel() This example model has two seperate validation annotations ( & ) which are going khổng lồ be used khổng lồ help validation your particular Mã Sản Phẩm against the data that it contains. Errors will be added to the ModelState, if it does not meet all of these requirements.
For example, if you were lớn have the following Controller Action :
public ActionResult Index(TestModel model) // Let"s assume that your ExampleProperty on your Model is null and your AnotherProperty field // exceeds the maximum number of characters allowed for it (10). // Validate it if(ModelState.IsValid) // Great! It passed validation (this won"t happen using the data mentioned above) else // Otherwise it failed validation (errors will be added khổng lồ the model) // Add your non-property errors ModelState.AddModelError("","One or more errors occurred."); return View(); this would spit out the following within your View if you were using a ValidationSummary element :
You seem to lớn have the right idea behind handling it, although it"s important lớn rethành viên that client-side validation and server-side validation (which this is) are two completely seperate but important concepts.