c# - 如何从 ASP.NET 中的表单访问字段数组?

标签 c# jquery asp.net .net

如果这个问题重复,请原谅我,因为我在 Stack Overflow 上的其他地方找不到这个问题的答案。

我在 ASP.NET 中有一个具有多个字段的表单,并且可以通过 jQuery 添加更多字段。在我的形式中,我想要一群 friend 。

这是 friend 类:

public class Friend {
    public string Name { get; set; }
    public int Age { get; set; }
}

这是我的表单,用户可以通过单击添加包含这两个字段的行的按钮来添加任意数量的 friend 。例如:

<table>
    <tr>
        <input type="text" name="friend[0].Name" />
        <input type="text" name="friend[0].Age" />
    </tr>
    <tr>
        <input type="text" name="friend[1].Name" />
        <input type="text" name="friend[1].Age" />
    </tr>
    <tr>
        <input type="text" name="friend[2].Name" />
        <input type="text" name="friend[2].Age" />
    </tr>
    ...
</table>

我如何在 Controller 的函数中将这些作为 friend 列表访问?

最佳答案

我想你已经快到了,

<form method="post" action="/Home/UpdateFriends">

    <input type="text" name="[0].Name" value="Curious George" />
    <input type="text" name="[0].Age" value="20" />

    <input type="text" name="[1].Name" value="Code Complete" />
    <input type="text" name="[1].Age" value="50" />



    <input type="submit" />
</form>

Controller 应该是这样的

//Action method on HomeController
public ActionResult UpdateFriends(ICollection<Friend> friends) {
    //your code here
}

this article更深入地解释它。

关于c# - 如何从 ASP.NET 中的表单访问字段数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36812030/

相关文章:

mysql - 在 ListView 中应用多个过滤器

c# - Observable.Create : CancellationToken doesn't transition to IsCancellationRequested

c# - 如何在 "boolean conditional statement"中进行循环?

c# - 将 EventSystem 用于按键事件

c# - WPF 图像停止重绘

jquery - 获取jquery datetimepicker到网格到分钟

php - 来自服务器的 Jquery 文件选择器

jquery - 如何使用 JQuery 选择当前悬停的元素?

javascript - 每次单击按钮时如何防止 javascript 重新加载?

c# - 在 GridView 中对 DropDownList 选定值使用 Eval