1

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?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
DTXqueque
  • 332
  • 2
  • 12
  • Are you using XML to input data? One way to validate a model is to build a schema for the xml input and then test the schema. – jdweng Dec 05 '18 at 10:47
  • @Rango, it's web forms. New to ASP.NET – DTXqueque Dec 05 '18 at 10:49
  • If it's Web-forms it isn't MVC, I guess ;) – Isma Dec 05 '18 at 10:49
  • @jdweng, I wanted to use hand-generated elements like , not DynamicData – DTXqueque Dec 05 '18 at 10:50
  • @Rango, i was doing this tutorial: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/updating-deleting-and-creating-data And then i wanted to replace with simple and run validation. – DTXqueque Dec 05 '18 at 10:55
  • The form is XAML so you can use the XAML schema (https://learn.microsoft.com/en-us/dotnet/framework/xaml-services/default-xaml-schema-context-and-wpf-xaml-schema-context). – jdweng Dec 05 '18 at 10:57
  • @jdweng the OP's link is to a WebForms tutorial, not XAML and definitely not MVC – Panagiotis Kanavos Dec 05 '18 at 11:18
  • @DTXqueque why are you using a *WebForms* tutorial when you want to create an MVC application? The two frameworks have very little in common. [This is the Validation article](https://learn.microsoft.com/el-gr/aspnet/mvc/overview/getting-started/introduction/adding-validation) in ASP.NET MVC's [Getting Started](https://learn.microsoft.com/el-gr/aspnet/mvc/overview/getting-started/introduction/) series – Panagiotis Kanavos Dec 05 '18 at 11:21
  • @PanagiotisKanavos, I dont want to create MVC application. 'Ragno' had just changed title and tag to mvc, dont know why :| – DTXqueque Dec 05 '18 at 11:24
  • @DTXqueque because WebForms was essentially abandoned 6-8 years ago. Explain what you want to do in the title and question itself, don't make people guess. And why learn *Web Forms* anyway? – Panagiotis Kanavos Dec 05 '18 at 11:39
  • @PanagiotisKanavos, I'm trying to implement admin panel for database of my Xamarin application. We decided to use WebForms to manage DB and then webservices to feed DB to the application. However what is best way to implement DB control panel nowadays? – DTXqueque Dec 05 '18 at 13:21

1 Answers1

0

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.

Johannes
  • 6,490
  • 10
  • 59
  • 108