簡體   English   中英

使用實體框架的MVC 4條件模型驗證

[英]MVC 4 Conditional Model Validation with Entity Framework

是否可以在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.Remove,但這沒有任何效果。

對此有何幫助將不勝感激?

根據您的注釋,您希望從數據庫填充模型,然后驗證它(因為它的舊數據可能無效),但不會根據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條件驗證

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM