简体   繁体   中英

How do I validate a MVC-Model?

From docs:

var item = new Product();
ModelState.AddModelError("", "User generated error");
TryUpdateModel(item);
if (ModelState.IsValid) ...

Now I want to fill model properties and then validate the model. Trying:

var item = new Product();

item.Name = Name.Text;
item.Price = Decimal.Parse(Price.Text);

TryUpdateModel(item); //Deletes Name and Price

How do I run validation in that way?

Your tutorial is based on xaml.

Generally speaking you can set validation properties in xaml. You either validate against the type that your element binds to or you implement an interface that allows a framework to validate the property with rules you supply or you notify the view if a dataerror occurs.

See an elaborate answer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM