2011-12-04 28 views
0

的服務器驗證我是很新,MVC3,並有一個關於在服務器端進行模型自動驗證的問題。MVC3 - 局部視圖及其相關模型

我的情況:有一個局部視圖 索引頁(可以稱之爲部分A)和相關的模型,比方說,客戶名稱。在這個局部視圖內部是另一個局部視圖(我們稱之爲局部B),它允許客戶輸入他們擁有自己模型的任何以前的名字(即,姓氏詳情)。

現在部分B是在用戶沒有輸入信息,除非他們想要的,而必須輸入部分A詳細介紹了可選的。

當按下提交按鈕時,其包圍兩個部分視圖的形式將觸發控制器和相關動作/方法 - MVC3自動驗證部分的模型。如果我將兩個模型作爲參數傳遞給動作,那麼兩者都會被驗證。

不過,我想驗證局部模型每次和客戶是否添加細節只是局部的B型。

所以,我想知道什麼是最好的方法,採取代碼這種情況。
我可以看到,通過檢查部分B上是否輸入了詳細信息,然後將操作/方法調用更改爲將兩個模型作爲輸入的調用,可以通過jquery/javascript更改表單標記屬性。這是最好的方式,還是有另一種更好的方式? 謝謝

回答

1

您可以禁用部分頁面b控件,並啓用它們,如果用戶想要輸入它們。因爲禁用控件不會被驗證。可能使用的另一個選項是使用條件驗證。你可以用google搜索條件驗證了asp.net的MVC,你會得到許多鏈接,引用您可能會看到here here

+0

謝謝 - 我會研究一下 – user1079925

0

繼我以前回答問題的意見稍有分歧,在評論該數據現在是user1079925聲明以單一模式提供。因此,我已經爲這種單一模型方法提供了使用自定義數據註釋的替代解決方案(以前的答案將被刪除):

此示例假定用戶將輸入名字,中間名和姓氏。如果輸入中間名或姓氏中的任何一個,則必須輸入姓氏。

型號:

public class IndexModel 
{ 
    [RequiredIfOtherFieldEntered("MiddleName", "Surname", ErrorMessage="Please enter the forename")] 
    public string Forename { get; set; } 

    public string MiddleName { get; set; } 

    public string Surname { get; set; } 
} 

IndexView:

@model MvcApplication6.Models.IndexModel 

<h2>Index</h2> 

@using (Html.BeginForm()) 
{ 
    <p>Forename: @Html.EditorFor(m => m.Forename) @Html.ValidationMessageFor(m => m.Forename)</p> 

    <p>If you enter the middle name or the surname then the forename will be required.</p> 

    <p>Middlename: @Html.EditorFor(m => m.MiddleName)</p> 

    <p>Surname: @Html.EditorFor(m => m.Surname)</p> 

    <input type="submit" value="submit"/> 
} 

HomeController中:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(IndexModel indexModel) 
    { 
     if (ModelState.IsValid) 
     { 
      return RedirectToAction("NextPage"); 
     } 
     else 
     { 
      return View(); 
     } 
    } 
} 

CustomAttribute:

public class RequiredIfOtherFieldEnteredAttribute : ValidationAttribute 
{ 
    private string[] properties; 

    public RequiredIfOtherFieldEnteredAttribute(params string[] properties) 
    { 
     if (properties == null && properties.Length < 1) 
     { 
      throw new ArgumentNullException("properties"); 
     } 

     this.properties = properties; 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     foreach (string property in properties) 
     { 
      //using System.Reflection.PropertyInfo; 
      PropertyInfo propertyInfo = validationContext.ObjectType.GetProperty(property); 

      if (propertyInfo == null) 
      { 
       return new ValidationResult(string.Format("Property '{0}' is undefined.", property)); 
      } 

      var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null); 

      if (propertyValue != null && !string.IsNullOrEmpty(propertyValue.ToString())) 
      { 
       if (value == null || string.IsNullOrEmpty(value.ToString())) 
        return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); 
      } 
     } 

     return null; 
    } 
}