ASP.NET MVC Beta中的模型绑定器支持List
csharppublic class Student{ public string Name { get; set; } public int Age { get; set; }}接下来,我们在Controller中定义一个接收Listcsharppublic ActionResult SaveStudents(List<Student> students){ // 在这里可以对students进行处理,比如保存到数据库中 return RedirectToAction("Index");}最后,在View中创建一个表单,用于输入学生信息,并将表单的提交目标指向SaveStudents Action方法。html@model List<Student>@using (Html.BeginForm("SaveStudents", "Home", FormMethod.Post)){ for (int i = 0; i < Model.Count; i++)</p> { <div> <label for="Name">姓名:</label> @Html.TextBoxFor(m => m[i].Name) </div> <div> <label for="Age">年龄:</label> @Html.TextBoxFor(m => m[i].Age) </div> } <input type="submit" value="提交" />}在这个示例中,我们使用了Html.TextBoxFor方法生成了一组输入框,通过m => m[i].Name和m => m[i].Age指定了每个输入框与ListCopyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号