【发布时间】:2020-05-04 21:14:45
【问题描述】:
我们有一套系统来管理英国建筑工匠的各个方面。
我们正在添加一个新部分来管理他们的认证,并希望简化我们当前的实施。
我们希望在 ViewModel 中有一个List<int>,但不确定如何从 View 中写入。请参阅下面的伪视图代码中的问题:
如果有帮助,我们会使用剑道。
视图模型
AccreditationApprovalViewModel.cs
public class AccreditationApprovalViewModel : ApprovalViewModel
{
// currently have:
// public bool HasAccreditation1;
// public bool HasAccreditation2;
// …
// public bool HasAccreditationN;
// We want to change the individual bools above to:
[DisplayName(“Has Accreditation“)]
public List<int> HasAccreditations;
}
查看
AccreditationApproval.cshtml
这里有许多布尔标志控件:
-
Accreditation1Flag
- 如果设置为 true,则 int 1 将添加到 ViewModel 中的 HasAccreditations 列表中(如果尚未在列表中)
- 如果设置为 false,则 int 1 将从 ViewModel 中的 HasAccreditations 列表中删除
-
Accreditation2Flag
- 如果设置为 true,则 int 2 将添加到 ViewModel 中的 HasAccreditations 列表中(如果尚未在列表中)
- 如果设置为 false,则 int 2 将从 ViewModel 中的 HasAccreditations 列表中删除
认证 3、4、5 等类似
我们的两个问题是:
- 我们如何创建 EditorFor 条目,以允许它写入 ViewModel 中的
List<int> - 我们如何将数字连接到 LabelFor 中的 DisplayString?
@Html.LabelFor(model => model.HasAccreditations.Concat(" 1"))
@Html.EditorFor(model => model.HasAccreditations[0]) @* bool control writing to int in array *@
@Html.LabelFor(model => model.HasAccreditations.Concat(" 2"))
@Html.EditorFor(model => model.HasAccreditations[1]) @* bool control writing to int in array *@
【问题讨论】:
-
您的实际问题是什么?我在这里看不到它。如果您有很多认证,我不会将它们存储在单独的
bools 中。在查找表中给每个人一个 ID 并创建多对多连接。 -
乔纳森,我已经修改了我的帖子,以便更清楚地了解我的问题。我们目前将我们的认证存储在个人
bools 中,但希望它们位于列表中。我们不确定视图中会发生什么
标签: c# asp.net asp.net-mvc razor