我有在我看來像下面的單選按鈕,列表,檢查在定製驗證的單選按鈕選擇的值..無法使用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)`
這種方式是正確的驗證單選按鈕選擇還是有任何其他方法,請提出任何想法。
會不會有人有任何想法如何檢查是否選擇了單選按鈕或不
很多感謝
有沒有人會對此提出任何建議.... –