无法检查控制器中的单选按钮选择值

时间:2013-09-12 12:01:26

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有4个单选按钮,我可以从下面的模型中绑定这些按钮

@model MVC.Models.DeleteModel
@using (@Html.BeginForm("Submit", "Delete"))
{

   <div class=".editor-field">
       @Html.Label("Delete");
   </div>

   <div class =".editor-field">
   @Html.RadioButtonFor(m=>m.IsDeleteByItemChecked,"By Item")
       @Html.Label("By Item")
  </div>
    <div class =".editor-field">
   @Html.RadioButtonFor(m=>m.IsDeleteByVendorChecked,"By Vendor")
         @Html.Label("By Vendor")

        @Html.RadioButtonFor(m=>m.IsCancelPageChecked,"Cancel Page")
         @Html.Label("Cancel Page")
  </div>
  <div class =".editor-field">
   @Html.RadioButtonFor(m=>m.IsDeleteByMemberChecked,"By Member")
       @Html.Label("By Member")
  </div>

    <div class=".editor-field">
           <input id="btnSubmitChanges" type="submit" value="Submit" />
        </div>    
}

这是我的模特

 public class DeleteModel
{

    public bool IsDeleteByItemChecked { get; set; }
    public bool IsDeleteByVendorChecked { get; set; }
    public bool IsDeleteByMemberChecked { get; set; }
    public bool IsCancelPageChecked { get; set; }

    [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; }

}

这是我的控制器

    [HttpPost]
    public ActionResult Submit(DeleteModel deleteModel)
    {

        //some coding stuff

         //here i am checking the radio button for selected one 
        if (deleteModel.IsDeleteByItemChecked)
        {

            DeleteItem deleteByItemInsert = new DeleteItem();
            deleteByItemInsert.DeleteField = "ITEM";
            deleteByItemInsert.DeleteIndicator = char.MinValue;
            deleteByItemInsert.DeleteValue = itemDTO.ItemID;
        }
        if (deleteModel.IsDeleteByMemberChecked)
        {
            DeleteItem deleteByMemberInsert = new DeleteItem();
            deleteByMemberInsert.DeleteField = "MEMBER";
            deleteByMemberInsert.DeleteIndicator = char.MinValue;
            deleteByMemberInsert.DeleteValue = "";
        }

问题是,如果我选择first radio button,然后点击submit button,如果控制器内有条件,但first if condition给出错误,则点击它{...

如何检查侧控制器中是否选择了单选按钮

任何人都可以为此找到解决方案......

另一个问题是,如果我选择第一个单选按钮,然后选择第二个单选按钮,我无法取消选择第一个单选按钮,我该如何纠正这个....

1 个答案:

答案 0 :(得分:1)

这两个问题是相关的。通过稍微修改模型,您可以对单选按钮进行分组,以便单击取消选择其他按钮以及检查所选的值。

首先,将您的模型修改为:

public class DeleteModel
{

    public string DeleteBy { get; set; }

    [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; }

}

Html.RadioButtonFor()的第一个参数应该是您正在使用的属性名称,第二个参数应该是该特定单选按钮的值。然后它们将具有相同的名称属性值,并且当/如果它与属性值匹配时,帮助器将选择给定的单选按钮。

继续修改DeleteModel类,这将使视图变为:

  <div class =".editor-field">
   @Html.RadioButtonFor(m=>m.DeleteBy,"item")
       @Html.Label("By Item")
  </div>
    <div class =".editor-field">
   @Html.RadioButtonFor(m=>m.DeleteBy,"vendor")
         @Html.Label("By Vendor")

   @Html.RadioButtonFor(m=>m.DeleteBy,"page")
         @Html.Label("Cancel Page")
  </div>
  <div class =".editor-field">
   @Html.RadioButtonFor(m=>m.DeleteBy,"member")
       @Html.Label("By Member")
  </div>

然后在控制器中检查,如下所示:

if (deleteModel.DeleteBy == "item")
{
    DeleteItem deleteByItemInsert = new DeleteItem();
    deleteByItemInsert.DeleteField = "ITEM";
    deleteByItemInsert.DeleteIndicator = char.MinValue;
    deleteByItemInsert.DeleteValue = itemDTO.ItemID;
}
if (deleteModel.DeleteBy == "member")
{
    DeleteItem deleteByMemberInsert = new DeleteItem();
    deleteByMemberInsert.DeleteField = "MEMBER";
    deleteByMemberInsert.DeleteIndicator = char.MinValue;
    deleteByMemberInsert.DeleteValue = "";
}