C# 如何创建列表<&燃气轮机;隐藏字段并将其提交给MVC中的控制器

C# 如何创建列表<&燃气轮机;隐藏字段并将其提交给MVC中的控制器,c#,jquery,asp.net-mvc,devexpress-mvc,C#,Jquery,Asp.net Mvc,Devexpress Mvc,我需要发回用户在DevExpress列表框中添加的项目,但根据该公司的说法,方法是将项目存储在隐藏字段中,然后提交。我需要知道如何在视图中创建这个隐藏字段,我认为它需要是一个包含文本和值的列表(类似于传递的模型),然后在jquery中如何为其分配值 注: 1.问题不在于如何创建隐藏字段,而在于该特定类型。 2.现在的情况是,在控制器中,模型返回为null // This code is located in the Index.cshtml page <div id="modalMain

我需要发回用户在DevExpress列表框中添加的项目,但根据该公司的说法,方法是将项目存储在隐藏字段中,然后提交。我需要知道如何在视图中创建这个隐藏字段,我认为它需要是一个包含文本和值的列表(类似于传递的模型),然后在jquery中如何为其分配值

注: 1.问题不在于如何创建隐藏字段,而在于该特定类型。 2.现在的情况是,在控制器中,模型返回为null

// 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);
}
//此代码位于Index.cshtml页面中
&时代;
//此代码位于ListBoxItemsModal.cshtml上
@模型列表
@使用(Html.BeginForm(“,”,FormMethod.Post,new{@id=“formPostListBoxItems”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.Label(“新项目文本”)
@TextBox(“name”,null,new{@id=“txtNewListBoxItem”})
添加项
@Html.DevExpress().ListBox(设置=>
{
settings.Name=“ListBoxCarMake”;
settings.Properties.EnableClientSideAPI=true;
settings.Properties.ValueField=“Value”;
settings.Properties.ValueType=typeof(字符串);
settings.Properties.TextField=“Text”;
}).BindList(Model).GetHtml()
}
//将新项目添加到列表框
$(document).on(“click”、“#btnAddListBoxItem”,函数(){s=$(“#txtNewListBoxItem”).val();ListBoxCarMake.AddItem;});
$(文档)。在(“单击”上,“#btnPostListBoxItems”,函数(e){
e、 预防默认值();
错误='';
$.ajax({
url:'@url.Action((“PostListBoxItems”,“System”),
cache:false,
类型:“POST”,
数据:$(“#formPostListBoxItems”).serialize(),
成功:函数(数据){$(“#modalMainData”).html(数据);},
错误:函数(xhr,状态,异常){DisplayAjaxError(xhr,状态,异常);}
});
});
//控制器
公共操作结果GetListOptions()
{
var模型=新列表();
添加(新的ValueText(){Text=“AUDI”,Value=“AUDI”});
添加(新的ValueText(){Text=“BMW”,Value=“BMW”});
添加(新的ValueText(){Text=“VW”,Value=“VW”});
返回PartialView(“~/Views/System/ListBoxItemsModal.cshtml”,model);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果PostListBoxItems(列表)
{
返回PartialView(“~/Views/System/ListBoxItemsModal.cshtml”,list);
}
@for(int i=0;iModel[i].Text)
@Html.HiddenFor(modeleItem=>Model[i].Value)
}

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

您更有可能使用
IEnumerable
方法
Count()
获取元素数。属性
Count
用于
IReadOnlyCollection
  @for (int i = 0; i < Model.Count; i++)
  {
        @Html.HiddenFor(modelitem => Model[i].Text)
        @Html.HiddenFor(modelitem => Model[i].Value)
  }