如何在 C# winform 应用程序中模拟 ModelState.IsValid 以进行任何模型验证
How to simulate ModelState.IsValid in C# winform application for any model validation
在 asp.net mvc 中,人们通过以下方式验证模型
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace MvcMovie.Models {
public class Movie {
public int ID { get; set; }
[Required]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required]
public string Genre { get; set; }
[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[StringLength(5)]
public string Rating { get; set; }
}
public class MovieDBContext : DbContext {
public DbSet<Movie> Movies { get; set; }
}
}
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
如何在任何 C# win 和 webform 应用程序中以相同的方式进行模型验证?
您可以使用 DataAnnotations 中可用的 ValidationContext 来执行此验证。您可能想制作自己的 class 以在 Web 应用程序中可用的一行代码中实现此目的。
var validationContext = new ValidationContext(movie, null, null);
var results = new List<ValidationResult>();
if (Validator.TryValidateObject(movie, validationContext, results, true))
{
db.Movies.Add(movie);
db.SaveChanges();
//Instead of a Redirect here, you need to do something WinForms to display the main form or something like a Dialog Close.
//return RedirectToAction("Index");
} else {
//Display validation errors
//These are available in your results.
}
根据 Parveen 的回答,我创建了一个 helper static class,可以重复使用:
public static class ModelState
{
public static List<string> ErrorMessages = new List<string>();
public static bool IsValid<T>(T model) {
var validationContext = new ValidationContext(model, null, null);
var results = new List<ValidationResult>();
if (Validator.TryValidateObject(model, validationContext, results, true))
{
return true;
}
else {
ErrorMessages = results.Select(x => x.ErrorMessage).ToList();
return false;
}
}
}
在您的 Form.cs
("Controller") 中,您可以这样称呼它:
private void btnSave_Click(object sender, EventArgs e)
{
var customerResource = GetViewModel();
if (ModelState.IsValid<CustomerResource>(customerResource)) {
}
}
private CustomerResource GetViewModel() {
return new CustomerResource() {
CustomerName = txtName.Text,
Phone = txtPhone.Text
};
}
所以这或多或少像 asp mvc now
在 asp.net mvc 中,人们通过以下方式验证模型
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace MvcMovie.Models {
public class Movie {
public int ID { get; set; }
[Required]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required]
public string Genre { get; set; }
[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[StringLength(5)]
public string Rating { get; set; }
}
public class MovieDBContext : DbContext {
public DbSet<Movie> Movies { get; set; }
}
}
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
如何在任何 C# win 和 webform 应用程序中以相同的方式进行模型验证?
您可以使用 DataAnnotations 中可用的 ValidationContext 来执行此验证。您可能想制作自己的 class 以在 Web 应用程序中可用的一行代码中实现此目的。
var validationContext = new ValidationContext(movie, null, null);
var results = new List<ValidationResult>();
if (Validator.TryValidateObject(movie, validationContext, results, true))
{
db.Movies.Add(movie);
db.SaveChanges();
//Instead of a Redirect here, you need to do something WinForms to display the main form or something like a Dialog Close.
//return RedirectToAction("Index");
} else {
//Display validation errors
//These are available in your results.
}
根据 Parveen 的回答,我创建了一个 helper static class,可以重复使用:
public static class ModelState
{
public static List<string> ErrorMessages = new List<string>();
public static bool IsValid<T>(T model) {
var validationContext = new ValidationContext(model, null, null);
var results = new List<ValidationResult>();
if (Validator.TryValidateObject(model, validationContext, results, true))
{
return true;
}
else {
ErrorMessages = results.Select(x => x.ErrorMessage).ToList();
return false;
}
}
}
在您的 Form.cs
("Controller") 中,您可以这样称呼它:
private void btnSave_Click(object sender, EventArgs e)
{
var customerResource = GetViewModel();
if (ModelState.IsValid<CustomerResource>(customerResource)) {
}
}
private CustomerResource GetViewModel() {
return new CustomerResource() {
CustomerName = txtName.Text,
Phone = txtPhone.Text
};
}
所以这或多或少像 asp mvc now