Javascript 在MVC中发送项目列表

Javascript 在MVC中发送项目列表,javascript,c#,jquery,asp.net-mvc,forms,Javascript,C#,Jquery,Asp.net Mvc,Forms,这是我的东西 public class RequestViewModel { public long FeederId { set; get; } public int A { set; get; } public int B { set; get; } public int C { set; get; } public int Remain { set; get; } } 这是我的Add模型,我想从我的表单发送到我的控制器 public

这是我的东西

public class RequestViewModel
{    
    public long FeederId { set; get; }

    public int A { set; get; }

    public int B { set; get; }

    public int C { set; get; }

    public int Remain { set; get; }
}
这是我的Add模型,我想从我的表单发送到我的控制器

public class RequestAddListViewModel
{
    public List<SuppliantRequestFeederAddViewModel> SuppliantRequestFeederAddViewModels { set; get; }

    public List<SelectListItem> FeederSelectListItems { set; get; }

    public long NodeId { set; get; }
}
我的jQuery脚本(已编辑)

var inputCount=0;
$(document).on('click', '.newfeeder', function () {
    inputCount++;
    var tr = $(this).closest("tr").clone();
    tr.find("input").val(0);
    tr.find("button").removeClass("btn-primary").addClass("btn-danger").removeClass("newfeeder").addClass("deleterow");
    tr.find("button i").removeClass("icon-plus2").addClass("icon-trash");
    tr.find("input,select").each(function () {
        $(this).attr({
            'name': function (_, name) { return name.toString().replace('0', inputCount) },
            'id': function (_, id) { return id.toString().replace('0', inputCount) }
        });

    });
    $(this).closest("tr").after(tr);
});

$(document).on('click', '.deleterow', function() {
    $(this).closest("tr").remove();
});

在我的表单中添加或删除新项目后,我终于找到了解决方案 我调用此函数
ReCreateIndex()


这意味着在对项目进行任何更改后,将重新创建项目索引。

您需要为集合索引器添加隐藏输入。参考因为您有一个dropdownlist,所以请使用
BeginCollectionItem()
方法。而您
var tr=$(this).clone()代码行永远无法正常工作,无法在您创建模型时绑定到您的模型submit@StephenMuecke,请查看已编辑的部分。您可以查看链接以了解如何正确操作:)@SalarAfshar,您的razor视图标记和JS需要确保下拉列表和文本框的名称格式为
SuppliantRequestFeederAddViewModels[].
其中
索引
按升序排列。@StephenMuecke我的问题是当我删除其中一个项目时,例如我有8个项目,当删除第3个项目并提交表单时,仅获取2个第一个项目,第3个项目后的所有项目不显示在控制器上
var inputCount=0;
$(document).on('click', '.newfeeder', function () {
    inputCount++;
    var tr = $(this).closest("tr").clone();
    tr.find("input").val(0);
    tr.find("button").removeClass("btn-primary").addClass("btn-danger").removeClass("newfeeder").addClass("deleterow");
    tr.find("button i").removeClass("icon-plus2").addClass("icon-trash");
    tr.find("input,select").each(function () {
        $(this).attr({
            'name': function (_, name) { return name.toString().replace('0', inputCount) },
            'id': function (_, id) { return id.toString().replace('0', inputCount) }
        });

    });
    $(this).closest("tr").after(tr);
});

$(document).on('click', '.deleterow', function() {
    $(this).closest("tr").remove();
});
function ReCreateIndex(container) {
    $(container).each(function (index, obj) {
        $("input,select", $(this)).each(function () {
            if ($(this).attr("name")) {
                var name = $(this).attr("name").replace($(this).attr("name").replace(/[^0-9]/gi, ''), index);
                $(this).attr("name", name);
            }

            if ($(this).attr("id")) {
                var id = $(this).attr("id").replace($(this).attr("id").replace(/[^0-9]/gi, ''), index);
                $(this).attr("id", id);
            }
        });
    });
}