2013-07-25 22 views
0

在我的控制器中,我有一個操作需要3個參數(primary_key,property和value)並使用反射來設置相應模型的值。 (通過它的主鍵找到)檢查實體框架中的模型錯誤

我想我可以趕上模型,如果它與ModelState.IsValid invlaid,但它評估爲真。現在它去到db.SaveChanges();,它引發異常。

ModelState有效。 (顯然它不是由主鍵找到的模型實例,實際上是指我的三個輸入)。

我以爲我可以檢查我的模型與下面的行錯誤...

if (System.Data.Entity.Validation.DbEntityValidationResult.ValidationErrors.Empty) 

但是,我得到一個「丟失的對象引用」錯誤。

我不知道這意味着什麼。 (C#的新手和其他一切。)有什麼幫助?

編輯1 - 顯示更多的代碼:

驗證

[Column("pilot_disembarked")] 
[IsDateAfter(testedPropertyName: "Undocked", 
      allowEqualDates: true, 
      ErrorMessage = "End date needs to be after start date")] 
public Nullable<System.DateTime> PilotDisembarked { get; set; } 

定製Validatior

public sealed class IsDateAfter : ValidationAttribute, IClientValidatable 
{ 
    private readonly string testedPropertyName; 
    private readonly bool allowEqualDates; 

    public IsDateAfter(string testedPropertyName, bool allowEqualDates = false) 
    { 
     this.testedPropertyName = testedPropertyName; 
     this.allowEqualDates = allowEqualDates; 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     var propertyTestedInfo = validationContext.ObjectType.GetProperty(this.testedPropertyName); 
     if (propertyTestedInfo == null) 
     { 
      return new ValidationResult(string.Format("unknown property {0}", this.testedPropertyName)); 
     } 

     var propertyTestedValue = propertyTestedInfo.GetValue(validationContext.ObjectInstance, null); 

     if (value == null || !(value is DateTime)) 
     { 
      return ValidationResult.Success; 
     } 

     if (propertyTestedValue == null || !(propertyTestedValue is DateTime)) 
     { 
      return ValidationResult.Success; 
     } 

     // Compare values 
     if ((DateTime)value >= (DateTime)propertyTestedValue) 
     { 
      if (this.allowEqualDates) 
      { 
       return ValidationResult.Success; 
      } 
      if ((DateTime)value > (DateTime)propertyTestedValue) 
      { 
       return ValidationResult.Success; 
      } 
     } 

     return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); 
    } 
} 

控制器動作

[HttpPost] 
    public ActionResult JsonEdit(string name, int pk, string value) 
    { 
     Voyage voyage = db.Voyages.Find(pk); 

     var property = voyage.GetType().GetProperty(name); 

     if (Regex.Match(property.PropertyType.ToString(), "DateTime").Success) 
     { 
      try 
      { 
       if (Regex.Match(value, @"^\d{4}$").Success) 
       { 
       var newValue = DateTime.ParseExact(value, "HHmm", System.Globalization.CultureInfo.InvariantCulture); 
       property.SetValue(voyage, newValue, null); 
       } 
       else if (value.Length == 0) 
       { 
        property.SetValue(voyage, null, null); 
       } 
       else 
       { 
       var newValue = DateTime.ParseExact(value, "yyyy/MM/dd HHmm", System.Globalization.CultureInfo.InvariantCulture); 
       property.SetValue(voyage, newValue, null); 
       } 
      } 
      catch 
      { 
       Response.StatusCode = 400; 
       return Json("Incorrect Time Entry."); 
      } 
     } 
     else 
     { 
      var newValue = Convert.ChangeType(value, property.PropertyType); 
      property.SetValue(voyage, newValue, null); 
     } 

     if (ModelState.IsValid) 
     { 
      db.SaveChanges(); 
      Response.StatusCode = 200; 
      return Json("Success!"); 
     } 
     else 
     { 
      Response.StatusCode = 400; 
      return Json(ModelState.Keys.SelectMany(key => this.ModelState[key].Errors)); 
     } 

    } 
+1

您需要向我們展示更多您的代碼。向我們展示引發異常的模型和函數。 – Amy

+0

@Amy - 添加了更多代碼...! – ovatsug25

+0

什麼行拋出異常? – user1477388

回答

0

當你的模型的任何值是零那個時候ModelState.IsValid。因此,請首先檢查您的模型數據。