0

我的视图中有单选按钮列表,如下所示..

看法:

  <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)` 

这种方式是否正确验证单选按钮选择或者是否有任何其他方法请提出任何想法。

有人知道如何检查单选按钮是否被选中

提前谢谢了

4

4 回答 4

0

从外观上看,在您的对象上设置的唯一属性是 Submit 、 ByItem 等。从未使用过。我认为您可以删除它们并将自定义验证器更改为仅使用 Submit 属性。

于 2014-01-15T21:59:48.383 回答
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; }
于 2013-10-15T11:53:24.527 回答
0

据我了解,您尝试验证是否选中了单选按钮。为什么不将[Required]属性添加到public SubmitAction Submit { get; set; }属性并删除IsRadionButtonSelected方法?

于 2013-10-15T11:29:55.057 回答
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); 
    }
于 2013-10-15T12:23:29.607 回答