Hỏi đáp

What Does Modelstate – Kiểm Tra Lỗi Modelstate – viettingame

When I do a create method i bind my object in the parameter and then I kiểm tra if ModelState is valid so I add to the database:

But when I need to change something before I add to the database (before I change it the ModelState couldn”t be valid so I have to do it)why the model state still non valid.

Đang xem: Modelstate

What does this function kiểm tra exactly?

This is my example:

public ActionResult Create( Encaissement encaissement) { encaissement.Montant = Convert.ToDecimal(encaissement.Montant); ViewBag.montant = encaissement.Montant; if (ModelState.IsValid) { db.Encaissements.Add(encaissement); db.SaveChanges(); return RedirectToAction(“Index”, “Encaissement”); }; ViewBag.ProjetID = new SelectList(db.Projets, “ProjetId”, “nomP”); return View(encaissement);}
c# asp.net-mvc
Share
Follow
edited Aug 7 “17 at 15:49

*

budi
5,47366 gold badges4646 silver badges7373 bronze badges
asked Apr 27 “16 at 15:08

*

kokomoikokomoi
53911 gold badge44 silver badges1313 bronze badges
3
Add a comment |

3 Answers 3

Active Oldest Votes
37
ModelState.IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.

In your example, the model that is being bound is of class type Encaissement. Validation rules are those specified on the model by the use of attributes, logic and errors added within the IValidatableObject”s Validate() method – or simply within the code of the action method.

The IsValid property will be true if the values were able to bind correctly to the model AND no validation rules were broken in the process.

Xem thêm: Link Tải 20 trò chơi Đánh Nhau Offline Pc Cực Hot, Tổng Hợp trò chơi Đối Kháng Offline Cho Pc

Here”s an example of how a validation attribute and IValidatableObject might be implemented on your model class:

public class Encaissement : IValidatableObject{ // A required attribute, validates that this value was submitted public int EncaissementID { get; set; } public DateTime? DateEncaissement { get; set; } public IEnumerable Validate(ValidationContext validationContext) { var results = new List(); // Validate the DateEncaissment if (!this.DateEncaissement.HasValue) { results.Add(new ValidationResult(“The DateEncaissement must be set”, new string { “DateEncaissement” }); } return results; }}Here”s an example of how the same validation rule may be applied within the action method of your example:

public ActionResult Create( Encaissement encaissement) { // Perform validation if (!encaissement.DateEncaissement.HasValue) { this.ModelState.AddModelError(“DateEncaissement”, “The DateEncaissement must be set”); } encaissement.Montant = Convert.ToDecimal(encaissement.Montant); ViewBag.montant = encaissement.Montant; if (ModelState.IsValid) { db.Encaissements.Add(encaissement); db.SaveChanges(); return RedirectToAction(“Index”, “Encaissement”); }; ViewBag.ProjetID = new SelectList(db.Projets, “ProjetId”, “nomP”); return View(encaissement);}It”s worth bearing in mind that the value types of the properties of your model will also be validated. For example, you can”t assign a string value to an int property. If you do, it won”t be bound and the error will be added to your ModelState too.

In your example, the EncaissementID value could not have a value of “Hello” posted to it, this would cause a model validation error to be added and IsValid will be false.

Xem thêm: Tải về trò chơi Titan Đại Chiến Titan trò chơi Trực tuyến, Titan Đại Chiến

It is for any of the above reasons (and possibly more) that the IsValid bool value of the model state will be false.

Về Viettingame.com

Viettingame.com - Chuyên trang web tổng hợp những thông tin hữu ích trên internet như thông tin về game, tin tổng hợp
Xem tất cả các bài viết của Viettingame.com →

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *