如何在 MVC 中创建一个 List<> 隐藏字段并将其提交给控制器

How to create a List<> hidden field and submit it to controller in MVC

我需要post返回用户在DevExpress ListBox中添加的项目,但是,根据公司的说法,方法是将项目存储在隐藏字段中,然后再提交.我需要知道如何在视图中创建这个隐藏字段,我认为它需要是一个带有文本和值的列表(类似于传递的模型),然后如何在 [=15 中为其分配值=].

备注: 1.问题不在于如何创建隐藏字段,而在于具体的类型。 2. 现在的方式是,在控制器中,模型返回为空。

// This code is located in the Index.cshtml page

<div id="modalMain" class="modal fade hidden-print" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="padding-bottom:0;padding-top:0">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div id="modalMainData" class="modal-body" style=" padding: 0 10px 0 10px !important;">
            </div>
        </div>
    </div>
</div>



// This code is located on ListBoxItemsModal.cshtml
@model List<ValueText>

    @using (Html.BeginForm("", "", FormMethod.Post, new { @id = "formPostListBoxItems" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class=" form-group">
            @Html.Label("New Item text")
            <div class="input-group">
                @Html.TextBox("name", null, new { @id = "txtNewListBoxItem" })
                <span class="input-group-btn">
                    <button id="btnAddListBoxItem" type="button" class="btn btn-default btn-xs">Add Item</button>
                </span>
            </div>
        </div>

        @Html.DevExpress().ListBox(settings =>
                {
                    settings.Name = "ListBoxCarMake";
                    settings.Properties.EnableClientSideAPI = true;
                    settings.Properties.ValueField = "Value";
                    settings.Properties.ValueType = typeof(string);
                    settings.Properties.TextField = "Text";
                }).BindList(Model).GetHtml()
    }
// Add a new item to list box
$(document).on("click", "#btnAddListBoxItem", function () { s = $("#txtNewListBoxItem").val(); ListBoxCarMake.AddItem(s); });

$(document).on("click", "#btnPostListBoxItems", function (e) {
    e.preventDefault();                            
    err = '';
    $.ajax({
        url: '@Url.Action(("PostListBoxItems", "System")',
        cache: false,
        type: "POST",
        data: $("#formPostListBoxItems").serialize(),
        success: function (data) { $("#modalMainData").html(data); },
        error: function (xhr, status, exception) { DisplayAjaxError(xhr, status, exception); }
    });
});

// CONTROLLER

public ActionResult GetListOptions()
{
    var model = new List<ValueText>();
    model.Add(new ValueText() { Text = "AUDI", Value = "AUDI" });
    model.Add(new ValueText() { Text = "BMW", Value = "BMW" });
    model.Add(new ValueText() { Text = "VW", Value = "VW" });

    return PartialView("~/Views/System/ListBoxItemsModal.cshtml", model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PostListBoxItems(List<ValueText> list)
{
    return PartialView("~/Views/System/ListBoxItemsModal.cshtml", list);
}
  @for (int i = 0; i < Model.Count; i++)
  {
        @Html.HiddenFor(modelitem => Model[i].Text)
        @Html.HiddenFor(modelitem => Model[i].Value)
  }

我建议您创建一个 ListContainer 并将其元素附加到您的 html 作为隐藏输入。这样,当按下提交按钮时,值将转到控制器。