【问题标题】:Pass List of models to controller ASP.NET MVC 5将模型列表传递给控制器​​ ASP.NET MVC 5
【发布时间】:2014-10-17 16:51:07
【问题描述】:

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

我的模特

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,然后当用户单击视图上的按钮时,会加载另一个 PartialView。

现在,假设我加载了两个部分视图,并且用户单击提交,我如何将具有这 3 个文本框的值的列表传递给控制器​​?

【问题讨论】:

  • 我想这就是你想要的......

标签: view model controller asp.net-mvc-5


【解决方案1】:

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

查看:-

只需添加任意数量的输入字段,但在动态添加输入时,请确保它们被正确命名和索引,如图所示:-

<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.
{....}

上面显示的示例是简单的 html 输入类型(易于实现),但如果您想要强类型文本框并动态添加它们,请查看此链接:- http://www.techiesweb.net/asp-net-mvc3-dynamically-added-form-fields-model-binding/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多