2009-12-31 37 views
0

有人可以解释为什么模型对象为null。我检查了通过网络发布的帖子值,并且它们都被填充。错误发布ASP.NET MVC中的编辑数据

使用VS2010 Beta 2,WinXp SP2,但是这在VS2008中工作??!

死亡消息的黄屏

Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 17:    <p> 
Line 18:     <label for="id">id:</label> 
Line 19:     <%= Html.TextBox("id", Model.id) %> <--Error 
Line 20:     <%= Html.ValidationMessage("id", "*") %> 
Line 21:    </p> 

控制器代码

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(Contact contactToEdit) 
    { 
     if (contactToEdit.FirstName.Trim().Length == 0) 
      ModelState.AddModelError("FirstName", "First name is required."); 
     if (contactToEdit.Lastname.Trim().Length == 0) 
      ModelState.AddModelError("LastName", "Last name is required."); 
     if (contactToEdit.Phone.Length > 0 && !Regex.IsMatch(contactToEdit.Phone, @"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}")) 
      ModelState.AddModelError("Phone", "Invalid phone number."); 
     if (contactToEdit.Email.Length > 0 && !Regex.IsMatch(contactToEdit.Email, @"^[\w-\.][email protected]([\w-]+\.)+[\w-]{2,4}$")) 
      ModelState.AddModelError("Email", "Invalid email address."); 

     if (!ModelState.IsValid) 
      return View(); 
     try 
     { 
      // TODO: Add update logic here 
      var con = (from c in _entities.Contacts 
         where c.id == contactToEdit.id 
         select c).FirstOrDefault(); 
      _entities.ApplyCurrentValues(con.EntityKey.EntitySetName, contactToEdit); 
      _entities.SaveChanges(); 


      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View(); 
     } 
    } 

摘录视图代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<JQGallery.Models.Contact>" %> 
... 
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %> 

<% using (Html.BeginForm()) {%> 

    <fieldset> 
     <legend>Fields</legend> 
     <p> 
      <label for="id">id:</label> 
      <%= Html.TextBox("id", Model.id) %> 
      <%= Html.ValidationMessage("id", "*") %> 
     </p> 
     <p> 
      <label for="FirstName">FirstName:</label> 
      <%= Html.TextBox("FirstName", Model.FirstName) %> 
      <%= Html.ValidationMessage("FirstName", "*") %> 
     </p> 
     <p> 
     ... 

回答

0

Model对象为空,因为您没有设置它。在您的POST控制器方法中,您返回的查看结果都不会设置模型(在if (!ModelState.IsValid)行之后并在catch块中)。在这两种情况下,您需要从某处重新加载模型,以便视图可以重新显示其数据。

+0

实际上,这不是真的,因为模型绑定。点击此处查看更多信息:http://www.asp.net/learn/mvc/tutorial-36-cs.aspx –

+0

模型绑定使用提交的表单值在OP的Edit方法中实例化和设置contactToEdit参数的属性。它没有实例化和填充可用于其View的Model对象。 (您教程中的示例链接仅使用View()方法工作,因为该特定示例视图不会尝试访问Model对象。) –

+0

实际上,我向您发送了一个链接,仅指向“创建”操作,但编辑将工作于同样的方式:http://www.asp.net/%28S%28pdfrohu0ajmwt445fanvj2r3%29%29/learn/mvc/tutorial-28-cs.aspx - 这个想法是因为模型POST'ed到行动,当动作简单地返回到视图时,模型绑定会将其推回到浏览器。 –

0

你有接受非员额动词和台控制器动作为你的观点提供模型?如果您没有响应GET请求并填充模型的Action方法直接导航到视图,模型将为空。

例如,你的正常操作可能是像

// 
// Reponds to: GET /Contact/Edit/5 

public ActionResult Edit(int id) 
{ 
    Contact contactToEdit = GetContactFromDatabase(id); 
    return View(contactToEdit); 
} 

我看不出什么毛病POST操作,但你需要一个相应的GET操作来设置形式为您服务。

编辑:你可能想考虑的另一件事情,因为你使用ASP.NET MVC内置模型绑定器是你应该排除ID(或任何敏感领域,用户不应该逻辑上能够更改)从被更改并实际从表单中删除(或至少使其不可编辑)。您可以使用传入联系人对象上的属性来完成此操作,如下所示:

// 
// Reponds to: POST /Contact/Edit/5 

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit([Bind(Exclude="id")] Contact contactToEdit) 
{ 
    // ... do edit logic/validation stuff 
} 
+0

我确实有一个GET操作,它带有标准的生成视图。 – D0cNet

+0

为什么它会在VS2008中工作? – D0cNet