[英]ASP.NET MVC - map bools in View to List of ints in ViewModel
我們有一個系統來管理英國建築商人的各個方面。
我們正在添加一個新部分來管理他們的認證,並希望簡化我們當前的實施。
我們希望在我們的 ViewModel 中有一個List<int>
,但不確定我們如何從我們的視圖中寫入它。 看下面偽View代碼中的問題:
如果它有幫助,我們正在使用 Kendo。
視圖模型
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
這里有多個 boolean 標志控件:
認證1標志
認證2標志
類似於認證 3、4、5 等
我們的兩個問題是:
List<int>
@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 *@
我認為你應該重組你的 model,否則你將永遠來回翻譯,你將無法輕松添加新的認證類型等。
一人一桌。 具有人員 ID 和認證 ID 的多對多中間表。 以及一個新的認證表,其中包含認證 ID 和認證標題。
如果您使用實體框架或類似框架,正確連接的表將輕松公開所有相關屬性,以便構建您的 model / 視圖 model。
對於您的觀點,如果您正在使用劍道,則可以使用他們的多重選擇器,或一系列以認證 ID 作為值和認證標題為 label 的復選框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.