当将对象列表从控制器传递到使用html.beginform()ASP.NET MVC时错误
我想将多个模型对象的多个实例作为列表发送到控制器,但是由于某种原因,我的代码给出了错误:system.argumentNullexception:'值不能为null。
这是我的观点:
@model List<MVCModel.Models.Student>
<b>Items</b> @ViewBag.Items <br />
<b>Value</b> @ViewBag.Value <br />
@using (Html.BeginForm("Form", "Home", FormMethod.Post))
{
<table>
<tbody id="GenaratorList">
<tr>
@{
for (int c = 0; c < Model.Count(); c++)
{
<td>Enter Name: </td>
<td>@Html.TextBoxFor(m => m[c].Name, new {id = "Name"})</td>
<td>Enter Age: </td>
<td>@Html.TextBoxFor(m => m[c].Age, new {id = "Age"})</td>
<td><input type="button" value="Add" onclick="addGenarator()" /></td>
}
}
</tr>
<tbody>
</table>
<input type="submit" value="Submit Form"/>
}
<script>
count = 0;
function addGenarator() {
var Name = $('#Name').val();
var Age = $('#Age').val();
var Student = "studentModels[" + count + "]";
$('#GenaratorList').append('<tr><td><input id="' + Student + '.Name" Name="' + Student + '.Name" type="text" class="form-control" readonly value="' + Name + '"></td><td><input id="' + Student + '.Age" Age="' + Student + '.Age" type="text" class="form-control" readonly value="' + Age + '"></td></tr>');
$('#Name,#Age').val('');
count++;
}
</script>
这是我的控制器:
[HttpPost]
public ActionResult Form(List<Student> s)
{
ViewBag.Items = s.Count;
ViewBad.Value = s[0].Age
return View("Index");
}
这是我的模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCModel.Models
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
}
I want to get multiple instances of my model objects sent to the controller as a list but for some reason, my code gives the error:System.ArgumentNullException: 'Value cannot be null.
This is my View:
@model List<MVCModel.Models.Student>
<b>Items</b> @ViewBag.Items <br />
<b>Value</b> @ViewBag.Value <br />
@using (Html.BeginForm("Form", "Home", FormMethod.Post))
{
<table>
<tbody id="GenaratorList">
<tr>
@{
for (int c = 0; c < Model.Count(); c++)
{
<td>Enter Name: </td>
<td>@Html.TextBoxFor(m => m[c].Name, new {id = "Name"})</td>
<td>Enter Age: </td>
<td>@Html.TextBoxFor(m => m[c].Age, new {id = "Age"})</td>
<td><input type="button" value="Add" onclick="addGenarator()" /></td>
}
}
</tr>
<tbody>
</table>
<input type="submit" value="Submit Form"/>
}
<script>
count = 0;
function addGenarator() {
var Name = $('#Name').val();
var Age = $('#Age').val();
var Student = "studentModels[" + count + "]";
$('#GenaratorList').append('<tr><td><input id="' + Student + '.Name" Name="' + Student + '.Name" type="text" class="form-control" readonly value="' + Name + '"></td><td><input id="' + Student + '.Age" Age="' + Student + '.Age" type="text" class="form-control" readonly value="' + Age + '"></td></tr>');
$('#Name,#Age').val('');
count++;
}
</script>
This is my Controller:
[HttpPost]
public ActionResult Form(List<Student> s)
{
ViewBag.Items = s.Count;
ViewBad.Value = s[0].Age
return View("Index");
}
This is my model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCModel.Models
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)