无法检查控制器中的单选按钮选定值
本文关键字:单选按钮 检查 控制器 | 更新日期: 2023-09-27 18:31:00
我有 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
它首先出现错误....
如何检查侧控制器中是否选择了单选按钮
请问有人对此有解决方案吗...
另一个问题是,如果我选择第一个单选按钮,然后选择第二个单选按钮,我无法取消选择第一个单选按钮,我该如何纠正这个....
这两个问题是相关的。 通过稍微修改模型,您将能够对单选按钮进行分组,以便单击取消选择其他按钮以及检查所选值。
首先,将模型修改为:
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 = "";
}