簡體   English   中英

帶動態字段的ASP.NET MVC表單

[英]ASP.NET MVC Form with Dynamic Fields

問題

我想顯示一個具有可變數值的表單。 但是,每個表單項都必須具有與之關聯的一種鍵。 例如,表單生成一個名稱,用戶名和職業字段,我還需要生成密鑰,以便在提交表單時,接收方法可以告訴哪些項目是什么。

試圖解決方案

現在我有一個FieldItems列表,其中FieldItem只是一個帶有一個String類變量的類。 填充表單時解決方案有效,但是在提交時會返回一個列表,我無法分辨哪個值屬於哪個鍵。 我正在考慮使用字典,但我不知道如何用剃刀完成。

查看模型

public class BuildReportViewModel
{
    public IList<BuildReportFieldItemViewModel> Fields { get; set; }
}
public class BuildReportFieldItemViewModel
{
    public string Field { get; set; }
}

BuildReportFieldItemViewModel的編輯器

@model BuildReportFieldItemViewModel

<div class="editor-label">
    <label for="@Model.Field">@Model.Field</label>
</div>
<div class="editor-field">
    <input type="text" name="@Model.Field">
</div>

嘗試使用隱藏的密鑰字段

查看模型

public class BuildReportViewModel
{
    public IList<BuildReportFieldItemViewModel> Fields { get; set; }
}
public class BuildReportFieldItemViewModel
{
    public string Field;
    public string Key;
}

BuildReportFieldItemViewModel的編輯器

@model BuildReportFieldItemViewModel

<div class="editor-label">
    <label for="@Model.Field">@Model.Field</label>
</div>
<div class="editor-field">

    @Html.TextBoxFor(x => x.Field)
    @Html.Hidden(Model.Key, "key_value");
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM