- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 C# 中使用 ASP.Net MVC。我有一个模型,其中有一个过滤条件成员。这个成员是一个 IList>。该键包含要显示的值,该值表明是否选择了此过滤器。我想将其绑定(bind)到我 View 中的一堆复选框。我就是这样做的。
<% for(int i=0;i<Model.customers.filterCriteria.Count;i++) { %>
<%=Html.CheckBoxFor(Model.customers.filterCriteria[i].value)%>
<%=Model.customers.filterCriteria[i].key%>
<% } %>
这会正确显示所有复选框。但是当我在 Controller 中提交我的表单时,无论我在 View 中选择什么,我都会得到 null 的过滤条件。
来自 this发布我得到创建单独属性的提示。但这对 IList 将如何工作……?有什么建议吗?
最佳答案
KeyValuePair<TKey, TValue>
的问题structure 是它具有私有(private) setter ,这意味着默认模型绑定(bind)器无法在 POST 操作中设置它们的值。它有一个特殊的构造函数,需要使用它来允许传递键和值,但是默认模型绑定(bind)器当然不知道这个构造函数并且它使用默认的,所以除非你为这种类型编写一个自定义模型绑定(bind)器你将无法使用它。
我建议您使用自定义类型而不是 KeyValuePair<TKey, TValue>
.
一如既往,我们从 View 模型开始:
public class Item
{
public string Name { get; set; }
public bool Value { get; set; }
}
public class MyViewModel
{
public IList<Item> FilterCriteria { get; set; }
}
然后是一个 Controller :
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel
{
FilterCriteria = new[]
{
new Item { Name = "Criteria 1", Value = true },
new Item { Name = "Criteria 2", Value = false },
new Item { Name = "Criteria 3", Value = true },
}
});
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// The model will be correctly bound here
return View(model);
}
}
和对应的~/Views/Home/Index.aspx
查看:
<% using (Html.BeginForm()) { %>
<%= Html.EditorFor(x => x.FilterCriteria) %>
<input type="submit" value="OK" />
<% } %>
最后我们为 ~/Views/Shared/EditorTemplates/Item.ascx
中的 Item 类型编写了一个定制的编辑器模板。或 ~/Views/Home/EditorTemplates/Item.ascx
(如果此模板仅特定于 Home Controller 且未重复使用):
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.Item>"
%>
<%= Html.CheckBoxFor(x => x.Value) %>
<%= Html.HiddenFor(x => x.Name) %>
<%= Html.Encode(Model.Name) %>
我们已经完成了两件事:清理丑陋的 View for
循环并使模型绑定(bind)器成功绑定(bind) POST 操作中的复选框值。
关于asp.net-mvc-2 - 将 KeyValuePair 列表绑定(bind)到复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676344/
我有一个 List> .我想知道 KeyValuePair谁拥有最大值 Key . 但是,我想获取 key 的值。不仅是 key 。 我能做到: int myValue = myListe.Max(
C#我在我的主 KeyValuePair 中使用 KeyValuePair 作为键。这是我的问题: 如果我有“KeyValuePair”,声明如下: KeyValuePair varName = ne
在 C# 中,如何将 KeyValuePair 转换为 KeyValuePair?我尝试了以下但它不起作用: public static KeyValuePair DoTheCast(KeyValue
以这种糟糕的数据结构结束: List>> 它不太可能变得很大(我估计 > 这很容易在 .NET 2.0 中编写 - 它基本上只是一个值的三元组,而不是在 KeyValuePair 中有 2 个。不过,
查看System.Collections.Generic.Dictionary , 它清楚地实现了 ICollection> ,但没有所需的“void Add(KeyValuePair item)”功
是否可以使用自定义结构来代替 KeyValuePair? 而不是 For Each kvp As KeyValuePair(Of class1,class2) In myDict Dim c1 A
我们如何验证 (.NET 2) 如果 KeyValuePair是否已分配值? Dim pair as KeyValuePair(Of A, B) = Nothing if pair.??? Then
我的 list返回 显然。如何在我的 KeyValuePair 中附加第二个字符串并添加我从第二次数据库调用返回的 ID? // Database calls here KeyValu
编辑:文化 这些内部操作表明不变文化 OrdinalIgnoreCase最合适。 KeyValuePair 是密封的。如果我有 var a = KeyValuePair("foo",1)和 var b
我正在用 C#/.NET 4.5 编写一个应用程序,我有一个定义如下的字典: Dictionary d = new Dictionary(); 我的整数键是连续且唯一的,除了空字符串外,我的值也将是唯
我正在执行 LINQ 查询以从数据库、年龄和日期中获取 2 个字段中的所有值。我想在图表上显示 5 个最高年龄的数字。为此,我试图将年龄和日期存储在 2 个单独的列表 ageList 和 dateLi
我有一个从数据库中填充的字典。 如果只返回一条记录,如何访问KeyValuePair? 我有这个代码: Dictionary CandidatePlatypiDict = PlatypusID > 0
假设我有 List> d 我知道字符串,但我想找到它的整数。如何在此列表中找到键值对? 最佳答案 你会这样写: var result = d.Where(kvp => kvp.Value == "s
我有一个具有 KeyValuePair 类型属性的对象。 我想从数据库中读取一些数据并将结果存储在这个 KeyValuePair 类型字段中。 myObject.KeyValuePairs = ctx
我有一个 IEnumerable对于具有 string 的实体和一个 int成员。 我需要将其转换为 KeyValuePair 的数组反之亦然。 它因转换错误而失败。 [DataContract] p
我有一个 IEnumerable> keyValueList 类型的对象, 我正在使用 var getResult= keyValueList.SingleOrDefault(); if(getR
我正在 build 一个 Dictionary带 key 型KeyValuePair .使用时KeyValuePair.Create内Dictionary初始化程序它不需要模板类型并且它正确地推断了类
我有一个列表 KeyValuePair在 C# 中格式为 KeyValuePair .我想从列表中删除具有重复值的项目。 具有 {X,Y} 的 Point 对象坐标。 示例数据: List> Data
我有这个代码: List>> aux = retEmpty.ToList(); aux.ForEach(x => ret.Add(x.Key, x.Value)); 我需要按第一个元素(“string
我有一个方法需要以下内容: public static List ParetoBuildBySum(List> inputData) 我有以下 linq 查询,并希望传入 KeyValueP
我是一名优秀的程序员,十分优秀!