我的视图中有单选按钮列表,如下所示..
查看:
<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;
}
}
我的问题是我没有检查单选按钮是否被选中,即使我选择单选按钮,此方法也会返回false值
private bool IsRadionButtonSelected(ValidationContext validationContext, string PropertyToSelect)`
这种方式对于验证单选按钮选择是否正确,或者是否有其他方法请提出任何建议。
是否有人知道如何选择是否选择了单选按钮
非常感谢提前
答案 0 :(得分:0)
据我了解,您尝试验证是否已选中单选按钮。为什么不将[Required]
属性添加到public SubmitAction Submit { get; set; }
属性并删除IsRadionButtonSelected
方法?
答案 1 :(得分:0)
您访问该媒体资源的验证码非常正确。它为null,因为您没有在视图中使用ByItem。而不是字符串使用布尔值。这应该有用。
更新
@Html.RadioButtonFor(m => m.ByItem, Model.ByItem) @Html.Label("Delete By Item")
[DeleteByItemValidator("ByItem")]
[Display(Name = "By Item")]
public bool ByItem { get; set; }
答案 2 :(得分: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);
}
答案 3 :(得分:0)
从它的外观来看,在你的对象上设置的唯一属性是Submit,ByItem等。从不使用。您可以删除它们,并将自定义验证程序更改为仅使用“提交”属性。