1

私は親モデル「領収書」を持っており、それはオブジェクトの辞書をCustomControlDictionaryクラスのプロパティとして持っていますCustomControlModel。私の見解では、CustomControlDictionaryプロパティ内の各オブジェクトをループして、EditorTemplate を呼び出します。テンプレートでは、CustomControlModelオブジェクトのラベルとテキスト ボックスを追加するだけです。コードが HTML コントロールをレンダリングするとき、同じ属性idname属性があります: 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とがあります。nameCustomControlDictionaryReceiptModel

<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
4

1 に答える 1

0

この状況に対する最善のアプローチは、テンプレートで HtmlHelper メソッドを使用せず、代わりに html コントロールを使用することだと思います。

@model EWMS_MVC.Classes.CustomControlModel
<label>@Model.LabelCaption</label>
<input id="@Model.CustomControlId" name="@Model.CustomControlName" type="text" value="@Model.Value" />
于 2016-01-05T23:51:26.547 に答える