[英]How can I get multiple selections from ASP.NET CheckBoxs
This seems like it should be prettty easy - but I just can't get it to work! 这看起来应该很简单-但我无法使其正常工作!
I have an enum in my model, which I want to display as a list of checkboxes. 我的模型中有一个枚举,我想将其显示为复选框列表。 The user can select multiple checkboxes, and I want to save this in the database. 用户可以选择多个复选框,我想将其保存在数据库中。
So the enum is like so (approx 20 elements unabridged): 所以枚举是这样的(大约20个元素未删节):
public enum ReferrerType
{
[Description("No Data")]
NoData = 9999,
[Description("Father or Mother")]
Parents = 1,
[Description("Brother or Sister")]
Sibling = 2,
[Description("Other")]
Other = 10
}
Whereby the Description is what is shown on the UI, and the numeric value is what is to be saved in the database. 其中,描述是在UI上显示的内容,数字值是要保存在数据库中的内容。 The numbers have to remain as listed, as they go directly into a stats package. 数字必须直接保留在列表中,因为它们直接进入统计数据包。
I then defined a Referrer class: 然后,我定义了Referrer类:
public class Referrer
{
public virtual Guid Id { get; private set; }
public virtual ReferrerType{ get; set; }
}
I realise this might be an odd (anti)pattern. 我意识到这可能是一个奇怪的(反)模式。 I developed it in haste, and am repenting at leisure. 我匆忙开发了它,并在闲暇时悔改。 Any advice on improving this model would also be much appreciated! 任何有关改进此模型的建议也将不胜感激!
My controller sets up the list: 我的控制器设置了列表:
private static IList<string> GenerateReferrerList()
{
var values = from ReferrerType e in Enum.GetValues(typeof(ReferrerType))
select new { Name = e.ToDescription() };
return values.Select(x => x.Name).ToList();
}
And I use it in my View like this: 我在视图中这样使用它:
<div class="radio-boolean form-field" id="Referrers">
<p class="form-field-attribute"> <span class="label">Referred By </span> </p>
<% for (var i = 0; i < ((IList<string>)ViewData["ReferrerList"]).Count; i++)
{ %>
<p class="form-field-value">
<%= Html.CheckBox(string.Format("Referrers[{0}].Type", i) ) %>
<label for="Referrers"> <%= ((IList<string>)ViewData["ReferrerList"])[i]%></label>
</p>
</div>
And it doesn't work! 而且它不起作用! I guess I'm missing something obvious, but I can't work out what. 我想我缺少明显的东西,但是我无法解决。 There are no errors - just an empty database table where referrers should be... 没有错误-应该是引荐来源网址为空的数据库表...
As always, any help much appreciated! 一如既往,任何帮助深表感谢!
Let's take a moment and see what do we need here. 让我们花点时间在这里看看我们需要什么。 We need to show a form which will contain multiple checkboxes (one for each value of the enum) and an associated label (this label should come from the Description attribute use on the enum). 我们需要显示一个表单,其中包含多个复选框(每个枚举的值一个)和一个关联的标签(此标签应来自枚举上的Description属性)。 When this form is submitted we want to fetch all the values that the use has checked. 提交此表单后,我们希望获取使用检查过的所有值。
So as always once we have clear definition of what we are trying to do we introduce our view model: 因此,一如既往,一旦我们对要执行的操作有了明确的定义,就可以引入视图模型:
public class MyViewModel
{
public bool IsChecked { get; set; }
public ReferrerType ReferrerType { get; set; }
public string Text { get; set; }
}
Then we write a controller: 然后我们编写一个控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = Enum.GetValues(typeof(ReferrerType)).Cast<ReferrerType>().Select(x => new MyViewModel
{
ReferrerType = x,
Text = x.ToDescription() // I guess that's an extension method you wrote
});
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable<MyViewModel> model)
{
...
}
}
And finally a strongly typed view corresponding to the Index
action of our controller (~/Views/Home/Index.aspx): 最后是一个与我们的控制器的Index
操作相对应的强类型视图(〜/ Views / Home / Index.aspx):
<% using (Html.BeginForm()) { %>
@Html.EditorForModel()
<input type="submit" value="OK" />
<% } %>
and the last part is the corresponding editor template ( ~/Views/Home/EditorTemplates/MyViewModel.ascx
): 最后一部分是相应的编辑器模板( ~/Views/Home/EditorTemplates/MyViewModel.ascx
):
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" %>
<%= Html.CheckBoxFor(x => x.IsChecked) %>
<%= Html.HiddenFor(x => x.ReferrerType) %>
<label><%: Model.Text %></label>
Now when this form is submitted inside the POST index action you would get a list of all enums with a corresponding boolean value indicating whether the user checked it or not. 现在,当在POST索引操作中提交此表单时,您将获得所有枚举的列表,并带有一个相应的布尔值,指示用户是否检查了它。
OT: Don't perform excess actions: OT:请勿执行过多的操作:
return (from e in Enum.GetValues(typeof(ReferrerType))
select e.ToDescription()).ToList();
or just 要不就
return Enum.GetValues(typeof(ReferrerType)).Select(e => e.ToDescription()).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.