我可能在这里是个白痴,但是我无法获得是否选中/选中复选框的价值。这是我到目前为止所得到的:
在我的模型中:
public IEnumerable<SelectListItem> Insurers
{
get
{
var list = new List<SelectListItem>();
string zInsurersList = "Age UK,Be Wiser,Call Connection,Churchill,Sainsbury's,Direct Line,Hastings Direct,LV=,Nationwide,RIAS,Swinton";
string[] zInsurers = zInsurersList.Split(',');
foreach (string aInsurer in zInsurers)
{
list.Add(new SelectListItem() { Text = aInsurer, Value = aInsurer, Selected=false});
}
return list;
}
}
}
我的观点:
@foreach (var insurer in @Model.Insurers)
{
var zInsurer = insurer.Text;
var zValue = insurer.Value;
<tr>
<td style="width: 120px; height: 35px;"><span id="@zInsurer">@zInsurer</span></td>
<td style="width: 40px; height: 35px;"><input id="@zInsurer" type="checkbox" name="@zInsurer"></td>
</tr>
}
所以在我的控制器中我试图循环列表并获取用户是否选择了该选项的值:
foreach (var item in model.Insurers)
{
//if (item.GetType() == typeof(CheckBox))
//string controlVal = ((SelectListItem)item).Selected.ToString();
zInsurers = zInsurers + item.Text + " " + ((SelectListItem)item).Selected.ToString() + "<br/>";
}
但值总是返回false。
有人可以花几分钟时间来突出我的愚蠢吗?
谢谢,
克雷格
答案 0 :(得分:2)
有很多方法可以做到这一点。我通常在模型中添加 String Array 来收集选定的值。
public string[] SelectedInsurers { get; set; }
<input type="checkbox" name="SelectedInsurers" value="@insurer.Value" />
以下是示例代码 -
public class MyModel
{
public string[] SelectedInsurers { get; set; }
public IEnumerable<SelectListItem> Insurers
{
get
{
var list = new List<SelectListItem>();
string zInsurersList = "Age UK,Be Wiser,Call Connection,Churchill,Sainsbury's,Direct Line,Hastings Direct,LV=,Nationwide,RIAS,Swinton";
string[] zInsurers = zInsurersList.Split(',');
foreach (string aInsurer in zInsurers)
{
list.Add(new SelectListItem { Text = aInsurer, Value = aInsurer, Selected = false });
}
return list;
}
}
}
public ActionResult Index()
{
return View(new MyModel());
}
[HttpPost]
public ActionResult Index(MyModel model)
{
return View();
}
@using (Html.BeginForm())
{
foreach (var insurer in @Model.Insurers)
{
<input type="checkbox" name="SelectedInsurers" value="@insurer.Value" /> @insurer.Text<br/>
}
<input type="submit" value="Post Back" />
}
答案 1 :(得分:2)
首先,您的属性Insurers
不应该是IEnumerable<SelectListItem>
(用于将集合绑定到下拉列表),并且在任何情况下,这种逻辑都不属于{{1 (以及创建逗号分隔的字符串然后拆分它的意义? - 只是首先创建一个字符串数组!)。它不是很清楚你想要做什么,但你应该创建一个视图模型,并以MVC方式并利用其模型绑定功能
查看模型
getter
控制器
public class InsurerVM
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
查看
public ActionResult Edit()
{
// This should be loaded from some data source
string[] insurers = new string[] { "Age UK", "Be Wiser", "Call Connection" };
List<InsurerVM> model = insurers.Select(i => new InsurerVM() { Name = i }).ToList();
return View(model);
}
发布方法
@model List<InsurerVM>
@using(Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(m => m[i].Name)
@Html.CheckBoxFor(m => m[i].IsSelected)
@Html.LabelFor(m => m.[i].IsSelected, Model[i].Name)
}
<input type="submit" value="Save" />
}
实际上,[HttpPost]
public ActionResult Edit(IEnumerable<InsurerVM> model)
{
// loop each item to get the insurer name and the value indicating if it has been selected
foreach(InsurerVM insurer in model)
{
....
}
}
将是一个具有Insurers
和其他属性的对象,因此可以识别它并与其他实体建立关系。
至于为什么你的代码不起作用。你的财产没有一个二传手,所以无论如何都不会发回任何东西。所有方法都在做的是初始化你的模型,然后调用getter创建一个新的ID
(与你首先发送给视图的那个相同)。并不是说它无论如何都很重要,你的复选框有IEnumerable<SelectListItem>
属性name
,name="Age_UK"
等,它们与你的模型完全没有关系,所以不能被绑定
答案 2 :(得分:0)
这是因为模型绑定无法处理您的值。
你应该研究模型绑定。
尝试这样的事情:
@for (var countInsurer = 0; Model.Insurers.Count > countInsurer++)
{
var zInsurer = insurer.Text;
var zValue = insurer.Value;
<tr>
<td style="width: 120px; height: 35px;"><span id="@zInsurer">@zInsurer</span></td>
<td style="width: 40px; height: 35px;">@Html.CheckBoxFor(m=> Model.Insurers[countInsurer], new {name = zInsurer})</td>
</tr>
}
答案 3 :(得分:0)
@for(int i = 0; i < Model.List.Count; i++)
{
@Html.CheckBoxFor(m => Model.List[i].IsChecked, htmlAttributes: new { @class = "control-label col-md-2" })
@Model.List[i].Name
@Html.HiddenFor(m => Model.List[i].ID)
@Html.HiddenFor(m => Model.List[i].Name)
<br />
}
控制器中的
StringBuilder sb = new StringBuilder();
foreach (var item in objDetail.List)
{
if (item.IsChecked)
{
sb.Append(item.Value + ",");
}
}
ViewBag.Loc = "Your preferred work locations are " + sb.ToString();
答案 4 :(得分:-1)
我从模块和权限表中获取模块如何将所有数据发送到RoleRight表所有复选框值
public class RoleRightModel
{
public ModuleModel _ModuleModel { get; set; }
public RightsModel _RightsModel { get; set; }
public RolesModel _RolesModel { get; set; }
public List<ModuleModel> _ModuleModelList { get; set; }
public List<RightsModel> _RightsModelList { get; set; }
public List<RolesModel> _RolesModelList { get; set; }
public List<RoleRightModel> _RoleRightModelList { get; set; }
public int RoleRightID { get; set; }
public int RoleID { get; set; }
public int ModuleID { get; set; }
public int FormMode { get; set; }
public int RightCode { get; set; }
public bool? RowInternal { get; set; }
public byte? IsAuthorised { get; set; }
public int? CreationID { get; set; }
public DateTime? CreationDate { get; set; }
public int? LastModificationID { get; set; }
public DateTime? LastModificationDate { get; set; }
public byte? RowStatus { get; set; }
public string RoleName { get; set; }
}
剃刀
@foreach(var item in Model._ModuleModelList.Where(x => x.Level == 1))
{
<ul style="display: block;">
<li><i class="fa fa-plus"></i>
<label>
@if (item.Level == 1)
{
<input id="node-0-1" data-id="custom-1" type="checkbox" name="Module" value="@item.ModuleID"@(Model._ModuleModel.ModuleID)? "checked":"">
@item.ModuleName
}
</label>
@foreach (var lavel1 in Model._ModuleModelList.Where(x => x.ParentModuleID == item.ModuleID))
{
<ul>
<li><i class="fa fa-plus"></i>
<label>
<input id="node-0-1-1" data-id="custom-1-1" type="checkbox" name="Module" value="@lavel1.ModuleID"@(Model._ModuleModel.ModuleID)? "checked":"">
@lavel1.ModuleName
</label>
@foreach (var lavel2 in Model._ModuleModelList.Where(x => x.ParentModuleID == lavel1.ModuleID))
{
<ul>
<li><i class="fa fa-plus"></i>
<label>
<input id="node-0-1-1-1" data-id="custom-1-1-1" type="checkbox" name="Module" value="@lavel2.ModuleID"@(Model._ModuleModel.ModuleID)? "checked":"">
@lavel2.ModuleName
</label>
@foreach (var lavel3 in Model._RightsModelList.Where(x => x.ModuleId == lavel2.ModuleID))
{
<ul>
<li>
<label>
<input id="node-0-1-1-1-1" data-id="custom-1-1-1-1" type="checkbox" name="Right" value="@lavel3.RightID"@(Model._RightsModel.RightID)? "checked":"">
@lavel3.RightName
</label>
</li>
</ul>
}
</li>
</ul>
}
</li>
</ul>
}
</li>
</ul>
}