使用实体框架的MVC 4条件模型验证

本文关键字:条件 模型 验证 MVC 实体 框架 | 更新日期: 2023-09-27 17:49:56

是否可以在ViewModel上放置条件,其中数据等于实体框架中的一个字段中的特定值,所需的元素或整个对象在ViewModel上使用TryValidateModel之前从ViewModel中删除?

我想从模型验证中删除HomeValue和PurchasePrice,其中OwnOrRent (Model .OwnOrRent)不等于1或9。

Model.cs

public class Address
{
    public int Id { get; set; }
    [DisplayName("House Name or Number")]
    [StringLength(50)]
    public string HouseNameOrNumber { get; set; }
    [DisplayName("Post Town")]
    [StringLength(50)]
    public string PostTown { get; set; }
    [Required(ErrorMessage = "Own or Rent is Required")]
    [DisplayName("Own or Rent")]
    [StringLength(50)]
    public string OwnOrRent { get; set; }
    [Required(ErrorMessage = "Mortgage/Rent Amount is Required")]
    [DisplayName("Mortgage/Rent Amount")]
    [StringLength(50)]
    public string MortgageRent { get; set; }
    [Required(ErrorMessage = "Home Value is Required")]
    [DisplayName("Home Value")]
    [StringLength(50)]
    public string HomeValue { get; set; }
    [Required(ErrorMessage = "Purchase Price is Required")]
    [DisplayName("Purchase Price")]
    [StringLength(50)]
    public string PurchasePrice { get; set; }
}

HomeController.cs

public ActionResult Application(int id)
{
    var viewAddressModel = new Address();
    using (
        var dbEntities = new DbEntities(new EntityConnection(_df.DbEntities)))
    {
        var model = dbEntities.Applications.Single(e => e.Id == id);
        viewAddressModel.Id = Id;
        viewAddressModel.HouseNameOrNumber = model.HouseNameOrNumber;
        viewAddressModel.PostTown = model.PostTown;
        viewAddressModel.OwnOrRent = GetStatus(model.OwnOrRent);
        viewAddressModel.MortgageRent = model.MortgageRent.ToString();
        viewAddressModel.HomeValue = model.HomeValue;
        viewAddressModel.PurchasePrice = model.PurchasePrice;
        if (model.OwnOrRent != "1" || model.OwnOrRent != "9")
        {
            ModelState.Remove("HomeValue");
            ModelState.Remove("PurchasePrice");
        }
        if (!TryValidateModel(viewAddressModel))
        {
            return PartialView("Address", viewAddressModel);
        }
    }
    var vm = new ApplicationViewModel { Item = CreateApp(id) };
    return PartialView("Application", vm);
}

正如您所看到的,我已经尝试使用ModelState。删除,但没有效果。

如果有任何帮助,我将不胜感激。

使用实体框架的MVC 4条件模型验证

根据你的评论,你想从数据库中填充一个模型,然后验证它(因为它的旧数据可能无效),但不显示基于OwnOrRent值的HomeValuePurchasePrice的错误,在这种情况下,你需要先调用TryValidateModel,然后删除ModelState错误

var viewAddressModel = new Address();
.... // set values
if (!TryValidateModel(viewAddressModel))
{
  if (model.OwnOrRent != "1" || model.OwnOrRent != "9")
  {
    if (ModelState.ContainsKey("HomeValue"))
    {
      ModelState["HomeValue"].Errors.Clear();
    }
    if (ModelState.ContainsKey("PurchasePrice"))
    {
      ModelState["PurchasePrice"].Errors.Clear();
    }
  }
}

您现在可以使用if (ModelState.IsValid)检查是否有任何其他验证错误,并返回适当的视图

旁注:我刚刚使用了与OwnOrRent值相关的if条件,但我怀疑您真正想要的是
if (!(model.OwnOrRent == "1" || model.OwnOrRent == "9"))

有一个线程关于不同的选项来做条件验证:ASP。NET MVC条件验证