私は親モデル「領収書」を持っており、それはオブジェクトの辞書をCustomControlDictionary
クラスのプロパティとして持っていますCustomControlModel
。私の見解では、CustomControlDictionary
プロパティ内の各オブジェクトをループして、EditorTemplate を呼び出します。テンプレートでは、CustomControlModel
オブジェクトのラベルとテキスト ボックスを追加するだけです。コードが HTML コントロールをレンダリングするとき、同じ属性id
とname
属性があります: id="key_Value"
name="key.Value"
. 辞書の代わりに通常の配列またはリストを使用してみましたが、結果は同じでした。アドバイスをお待ちしております。
モデル:
public class ReceiptModel
{
public ReceiptModel(){}
public int ReceiptId { get; set; }
public string ReceiptNumber { get; set; }
public Dictionary<string, CustomControlModel> CustomControlDictionary{get; set; }
// public List<CustomControlModel> CustomControlList { get; set; }
}
public class CustomControlModel
{
public CustomControlModel(){}
public int CustomControlId{ get; set; }
public string CustomControlName{ get; set; }
public string LabelCaption{ get; set; }
public string Value{ get; set; }
}
意見:
//View (@ReceiptModel):
@foreach (var key in Model.CustomControlDictionary )
{
@Html.EditorFor(m => key.Value,"CustomControlModel")
}
エディタ テンプレート:
@model EWMS_MVC.Classes.CustomControlModel
@Html.HiddenFor(model => model.CustomControlId)
@Html.LabelFor(model => model.CustomControlId, Model.LabelCaption)
@Html.TextBoxFor(model => model.CustomControlId, new { @Value = @Model.Value })
レンダリングされた html には、次のプロパティのすべてのオブジェクトに対して同じid
とがあります。name
CustomControlDictionary
ReceiptModel
<input id="key_Value_CustomControlId" name="key.Value.CustomControlId" type="hidden" value="201922" />//value here should be "id" of the input field
<label for="key_Value_CustomControlId">Receipt number:</label>
<input id="key_Value_CustomControlId" name="key.Value.CustomControlId" type="text" value="201922" />//value here should be "id" of the input field