2013-10-15 23 views
0

我有在我看來像下面的單選按鈕,列表,檢查在定製驗證的單選按鈕選擇的值..無法使用MVC

查看:

<div class="deleteControls"> 
    <div class="labelHead">@Html.Label("Delete")</div> 
    <div class="controlsAndLabels">@Html.RadioButtonFor(m => m.Submit, MVC.Models.SubmitAction.DeleteItem) @Html.Label("Delete By Item")</div> 
    <div class="controlsAndLabels">@Html.RadioButtonFor(m => m.Submit, MVC.Models.SubmitAction.DeleteVendor) @Html.Label("Delete By Vendor")</div> 
    <div class="controlsAndLabels" style="padding-left: 20px;">@Html.CheckBoxFor(m => m.IsCancelPageChecked, "Cancel Page") @Html.Label("Cancel Page")</div> 
    <div class="controlsAndLabels">@Html.RadioButtonFor(m => m.Submit, MVC.Models.SubmitAction.DeleteMember) @Html.Label("Delete By Member")</div> 
</div> 

,這是我的模型在那裏,我的單選按鈕定義屬性,查看

型號:

public SubmitAction Submit { get; set; } 
    public bool IsCancelPageChecked { get; set; } 

    [DeleteByItemValidator("ByItem")] 
    [Display(Name = "By Item")] 
    public string ByItem { get; set; } 

    [Display(Name = "By Vendor")] 
    public string ByVendor { get; set; } 

    [Display(Name = "By Member")] 
    public string ByMember { get; set; } 

    [Display(Name = "Cancel Page")] 
    public string CancelPage { get; set; } 

與此枚舉綁定的單選按鈕列表

public enum SubmitAction 
{ 
    DeleteItem, 
    DeleteVendor, 
    DeleteMember 

} 

我使用這樣的自定義的驗證下面

public class DeleteByItemValidator : ValidationAttribute 
{ 
    public string DeleteByItemRadioButton { get; set; } 

    public DeleteByItemValidator(string deleteByItemRadioButton) 
    { 
     this.DeleteByItemRadioButton = deleteByItemRadioButton; 
    } 

    protected override ValidationResult IsValid(object currentValue, ValidationContext validationContext) 
    { 
     if (IsRadionButtonSelected(validationContext, DeleteByItemRadioButton)) 
     { 
      // here I am doing validaions 
     } 
     return ValidationResult.Success; 
    } 

    // this method is giving always false even if i selected one radio button 
    private bool IsRadionButtonSelected(ValidationContext validationContext, string PropertyToSelect) 
    { 
     Type iType = validationContext.ObjectInstance.GetType(); 
     object RadioButtonSelectedValue = iType.GetProperty(PropertyToSelect).GetValue(validationContext.ObjectInstance, null);//here I am getting null value 
     bool isChecked = Convert.ToBoolean(RadioButtonSelectedValue); 
     return isChecked; 
    } 
} 

我的問題做自定義的驗證服務器端的是,我沒有檢查是否單選按鈕被選中與否,並且即使我選擇了單選按鈕,此方法也會返回虛假值

private bool IsRadionButtonSelected(ValidationContext validationContext, string PropertyToSelect)` 

這種方式是正確的驗證單選按鈕選擇還是有任何其他方法,請提出任何想法。

會不會有人有任何想法如何檢查是否選擇了單選按鈕或不

很多感謝

+0

有沒有人會對此提出任何建議.... –

回答

0

據我瞭解,您嘗試驗證一個單選按鈕被選中。爲什麼不將[Required]屬性添加到public SubmitAction Submit { get; set; }屬性並刪除IsRadionButtonSelected方法?

+0

是試圖驗證單選按鈕,如果我刪除,我怎麼能通過當前單選按鈕選擇項目驗證方法 –

+0

@pratapk這[頁] (http://dotnetspeak.com/2012/05/validating-dependent-fields-in-asp-net-mvc)幫助我處理了類似的情況。 – developer10214

+0

但它不包含單選按鈕列表.....在我的情況下,我有三個單選按鈕和一個提交按鈕,如果我選擇第一個單選按鈕,然後按提交按鈕,我需要在視圖上顯示相應的錯誤消息。 .. –

0

驗證器代碼在訪問屬性的位置是正確的。它是空的,因爲您沒有在視圖中使用ByItem。而不是一個字符串使用布爾值。這應該工作。

更新:

@Html.RadioButtonFor(m => m.ByItem, Model.ByItem) @Html.Label("Delete By Item") 

    [DeleteByItemValidator("ByItem")] 
    [Display(Name = "By Item")] 
    public bool ByItem { get; set; } 
+0

我在該視圖中有4個單選按鈕,在這種情況下,這個代碼的作品,你已經指定在視圖中... –

+1

是啊,我不認爲你需要一個自定義驗證,除非你有特殊要求。在你發佈後,類似於(!model.ByItem) ModelState.AddModelError(「ByItem」,「ByItem not selected」); – Spock

+0

你可以看到我的枚舉在該模型中的視圖我使用的是枚舉....如果用戶被選中,我需要做一些驗證與數據庫來自數據..我認爲這個我需要做自定義驗證... –

0

我會做沒有自定義驗證。例如:

型號

public class Test3Model 
{ 
    [Required] 
    public SubmitAction Submit { get; set; } 
    public bool IsCancelPageChecked { get; set; } 

} 

public enum SubmitAction 
{ 
    None, 
    ByItem, 
    ByVendor, 
    ByMember 
} 

查看

@using (Html.BeginForm("Test3","Form")) 
{ 
    @Html.RadioButtonFor(m => m.Submit, SubmitAction.ByItem, Model.Submit== SubmitAction.ByItem ? new { Checked = "checked" } : null) 
    @Html.RadioButtonFor(m => m.Submit, SubmitAction.ByVendor, Model.Submit== SubmitAction.ByVendor ? new { Checked = "checked" } : null) 
    @Html.RadioButtonFor(m => m.Submit, SubmitAction.ByMember, Model.Submit== SubmitAction.ByMember ? new { Checked = "checked" } : null) 


    @Html.ValidationSummary(); 
    <input type="submit"/> 
} 

控制器

[HttpPost] 
    public ActionResult Test3(Test3Model model) 
    { 
     if (ModelState.IsValid && model.Submit != SubmitAction.None) 
     { 
      //some actions 
     } 

     return View("Test3", model); 
    } 
0

從外觀上來看,這是你的對象上設置的屬性是提交,ByItem,等等。從來沒有使用過。您可以刪除它們,我認爲並更改自定義驗證程序僅使用提交屬性。