ASP.NET MVC通过Javascript将项目添加到控制器的列表中

ASP.NET MVC通过Javascript将项目添加到控制器的列表中,javascript,c#,asp.net,model-view-controller,Javascript,C#,Asp.net,Model View Controller,我的html页面中有一个ListBoxFor: @Html.ListBoxFor(model => model.SelectedProduct, new SelectList(Model.Products, "Value", "Text"), new { @class = "form-control col-12", @id = "produtosEnvolvidos" }) 我想使用户能够通过加号(+)按钮将项目添加到该列表中 一旦用户提交表单,我想检索控制器中所有新添加的项,以便更新

我的html页面中有一个ListBoxFor:

@Html.ListBoxFor(model => model.SelectedProduct, new SelectList(Model.Products, "Value", "Text"), new { @class = "form-control col-12", @id = "produtosEnvolvidos" })
我想使用户能够通过加号(+)按钮将项目添加到该列表中

一旦用户提交表单,我想检索控制器中所有新添加的项,以便更新数据库,但一旦我检查Model.Products列表,它总是空的,我想这是因为javascript实际上没有正确地与asp.net通信

型号:

[Display(Name = "Product")]
public string SelectedProduct { get; set; }
public IEnumerable<SelectListItem> Products { get; set; }
有什么解决办法吗


我可以使用JQuery和Ajax等技术。

MVC表单提交基于
name
属性,而不是
id
@Dwight,您在视图中使用的是什么类型的模型?模型是否有一个名为“产品”的列表?(我假设)。您必须有一个与您的模型期望的名称相同的控件,以便它可以填充它。model.Products是一个
IEnumerable
,因此,我更改了名称,使其与模型
产品
上的名称匹配,但在控制器内时仍返回null。我只能根据
名称
属性,而不是
id
@Dwight获取SelectedItemMVC表单提交工作,您在视图中使用的是哪种模型?模型是否有一个名为“产品”的列表?(我假设)。你必须有一个与你的模型期望的名称相同的控件,这样它才能填充它。model.Products是一个
IEnumerable
,因此我更改了名称,使其与model
Products
上的名称匹配,但在控制器内时仍然返回null。我只能获取SelectedItem
$(document).ready(function () {
    $("#addList").click(function () {
        var x = document.getElementById("produtosEnvolvidos");
        var option = document.createElement("option");
        option.text = document.getElementById("produtotxt").value;
        x.add(option);
    })
});
[Display(Name = "Product")]
public string SelectedProduct { get; set; }
public IEnumerable<SelectListItem> Products { get; set; }
model.Products = new SelectList(model._Products, "Product", "Text");