StudentModel。
namespace mvcApp.Models
{
public class StudentModel
{
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
public List<SchoolOrganization> Organizations { get; set; }
}
public class SchoolOrganization
{
public string Name { get; set; }
public bool IsInvolved { get; set; }
}
}
学生参与多个组织。
控制器
namespace mvcApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult StudentInformation()
{
// populate student with data
var student = new StudentModel() { FirstName = "Joe", LastName = "Doe", EmailAddress = "jdoe@hotmail.com"};
// Populate with Organizations
student.Organizations = new List<SchoolOrganization>();
student.Organizations.Add(new SchoolOrganization() { Name = "Math Club", IsInvolved = true});
student.Organizations.Add(new SchoolOrganization() { Name = "Chess Club", IsInvolved = false });
student.Organizations.Add(new SchoolOrganization() { Name = "Football", IsInvolved = true });
return View(student);
}
**[HttpPost]
public ActionResult StudentInformation(StudentModel student)
{
Response.Write("Name: " + student.FirstName);
foreach (var o in student.Organizations)
{
Response.Write(o.Name + " : " + o.IsInvolved.ToString());
}
return View();
}**
}
}
最终将从数据库填充数据。
查看
@model mvcApp.Models.StudentModel
@{
ViewBag.Title = "StudentInformation";
}
<h2>StudentInformation</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>StudentModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<div>
<table>
<tr>
<td>Organization Name</td><td>Is Involved</td>
</tr>
@for (int i = 0; i < Model.Organizations.Count; i++) <== System.NullReferenceException here
{
@Html.HiddenFor(m => m.Organizations[i].IsInvolved)
<tr>
<td>@Html.DisplayFor(m => m.Organizations[i].Name)</td>
<td>@Html.CheckBoxFor(m => m.Organizations[i].IsInvolved)</td>
</tr>
}
</table>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
上面的代码与HttGet一起显示正常。但是,当我尝试更新时,我得到System.NullReferenceException。 https://www.dropbox.com/s/dz0bg3hkd0yq8e3/studentInformation.png?dl=0
任何人都可以帮忙了解一下发生了什么事吗?
谢谢。
答案 0 :(得分:0)
在您提供的代码示例中; HomeController的[HttpPost] ActionResult for StudentInformation不会创建更新对象模型的新实例并将其传递给视图,而是运行基本的debug.writeline例程。因此,&#34; StudentInformation.cshtml&#34;的[HttpPost]视图的从属视图不会收到更新模型的填充实例...
在&#34; StudentInformation.cshtml&#34;模型的第一行设置断点。页面并运行应用程序将演示页面顶部引用的模型,其中没有数据......
这就是为什么页面的[HttpPost]版本只是呈现空白模型,而没有您可能创建的任何更改的数据值,UNTIL它到达视图依赖于必须的新数据值计数的部分出现在页面第一行调用的模型中......继续。
空数据模型集导致空引用,因为其中没有要计数的值。
为了查看更新的设置组,视图模型的[HttpPost]版本必须传递一个返回新信息的模型实例,如&#34; return View(nameOfViewDataSet)&#34; (您再次构建数据集,并将其作为模型的新版本传递,并提供修订的表单数据。)
直到通过返回View语句相对于StudentInformation ActionResult的[HttpPost]版本传递数据,到实际视图,将没有显示数据,并且count函数将继续返回空值。 / p>
我希望这有用。