View 将模型列表传递给控制器ASP.NET MVC 5

View 将模型列表传递给控制器ASP.NET MVC 5,view,model,controller,asp.net-mvc-5,View,Model,Controller,Asp.net Mvc 5,我有一个视图,用户可以在其中输入一些数据。但问题是: 假设视图中有3个文本框。每次用户可以多次填写这3个文本框。为了澄清,假设用户填写这3个文本框,然后按下按钮,再次在表单上添加这3个文本框。现在,当用户单击“提交”时,此表单将发送给控制器,但如何发送模型列表作为参数。 我针对这个问题的架构如下所示: MyModel public int ID { get;set; } public string Something { get; set; } /*This three textboxes

我有一个视图,用户可以在其中输入一些数据。但问题是: 假设视图中有3个文本框。每次用户可以多次填写这3个文本框。为了澄清,假设用户填写这3个文本框,然后按下按钮,再次在表单上添加这3个文本框。现在,当用户单击“提交”时,此表单将发送给控制器,但如何发送模型列表作为参数。 我针对这个问题的架构如下所示:

MyModel

public int ID { get;set; }

public string Something { get; set; }

/*This three textboxes can user set multiple times*/
/*Perhaps i Can create new model with these properties and then 
/*put List of that model as property here, but how to fill that list inside view ??*/                   
public string TextBoxOneValue { get; set; }   
public string TextBoxTwoValue { get; set; }
public string TextBoxThreeValue { get; set; }
现在,我想用这3个文本框创建PartialView,然后当用户单击view上的按钮时,另一个PartialView被加载


现在,假设我加载了两个部分视图,用户单击submit,我如何将包含这3个文本框值的列表传递给controller

您可以通过以下方式实现上述功能:

查看:-

Public ActionResult MyControllerAction(List<string> textdata) // here textdata will have list of posted input values.
{....}
只需添加所需数量的输入字段,但动态添加输入时,请确保正确命名和索引输入字段,如图所示:-

<input type="text" name="[0].textdata" value="1" />
<input type="text" name="[1].textdata" value="2" />
<input type="text" name="[2].textdata" value="3" />
<input type="text" name="[3].textdata" value="4" />
..........
..........

..........
..........
控制器:-

Public ActionResult MyControllerAction(List<string> textdata) // here textdata will have list of posted input values.
{....}
Public ActionResult mycontrolleration(List textdata)//这里textdata将包含已发布的输入值列表。
{....}

上面显示的示例使用简单的html输入类型(易于实现),但如果您想要强类型文本框并动态添加它们,请查看以下链接:-

我想这就是您想要的。。。