2014-01-09 78 views
0

我构建了一个编辑页面来更新数据,如果传递了正确的ID但传入的ID无效时,它会正常工作我得到一个空引用异常。我知道这是由于LINQ查询没有从数据库中找到任何有效数据的事实,但我不知道如何处理这个,除非我在我的视图中添加一堆IF语句,以便每次引用模型。这是我目前拥有的控制器代码。如果在模型的LINQ查询中找不到任何结果,则返回错误消息

public ActionResult EditSection(Int16 id = -1) 
    { 
     Section section = db.Sections.Find(id); 
     SectionAddEditVM model = new SectionAddEditVM { Section = section }; 

     if (section != null) 
     { 
      if (section.Type == "Collection") 
      { 
       RedirectToAction("Collection", new { id = id }); 
      } 

      model.SelectedType = section.Type; 
      return View(model); 
     } 

     ModelState.AddModelError("Section ID", "Invalid Section ID"); 
     return View(model); 
    } 

查看:

@model SectionAddEditVM 

@{ 
    ViewBag.Title = "Edit " + Model.Section.Title + " Information"; 
} 

<h2> 
    Edit @Model.Section.Title Information 
</h2> 

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken(); 
    @Html.ValidationSummary(false) 

    <p> 
     @Html.HiddenFor(m => m.Section.ID) 
     <label for="Title">Seciton Title:</label> @Html.EditorFor(m => m.Section.Title) 
     <br /> 
     <label for="RouteName">Section Route:</label> @Html.EditorFor(m => m.Section.RouteName) 
     <br /> 
     <label for="Type">Section Type:</label> @Html.DropDownListFor(m => m.Section.Type, new SelectList(Model.Type, "Value", "Text")) 
     <br /> 
     @Html.HiddenFor(m => m.Section.LogoFileID) 
     <label for="LogoFile">Logo Image:</label> <input id="LogoFile" name="LogoFile" type="file" /> 
     <br /> 
     <label for="Synopsis">Synopsis:</label> @Html.EditorFor(m => m.Section.Synopsis) 
     <br /> 
     <input type="submit" value="Edit Information" /> 
    </p> 
} 
+0

null参考异常在上面的代码中发生了什么?在新的SectionAddEditVM?或者你已经用if(section!= null)来解释它 – tofutim

+0

当我参考模型时,空引用异常发生在视图中。我添加了视图代码以供参考。 – Matthew

+0

为什么在视图中需要很多if语句?一个if语句会处理这个......'if(model == null){output message} else {do stuff}。或者,而不是模型状态错误,重定向到基本上说您的请求包含无效数据的错误视图。 – Tommy

回答

0

解决方案是添加ELSE子句并初始化一个新的空白模型。

public ActionResult EditSection(Int16 id = -1) 
    { 
     Section section = db.Sections.Find(id); 

     if (section != null) 
     { 
      if (section.Type == "Collection") 
      { 
       RedirectToAction("Collection", new { id = id }); 
      } 

      SectionAddEditVM model = new SectionAddEditVM { Section = section }; 
      model.SelectedType = section.Type; 
      return View(model); 
     } 
     else 
     { 
      section = new Section(); 
      SectionAddEditVM model = new SectionAddEditVM { Section = section }; 

      ModelState.AddModelError("Section ID", "Invalid Section ID"); 
      return View(model); 
     } 
    } 
+0

如果投票的人解释了原因,那会很好。没有人给我解决我发布的问题和我的解决方案。 – Matthew

0

在你的控制器你已经检查是否sectionnull。所以,如果它是IS null只是返回一个不同的观点,说:“找不到部分”或什么。另外(如这里由@Aron建议),你可以回到这个观点404状态代码,所以控制器将是这个样子:

// find section based on ID 
// ... actual code ... 
if (section != null) 
{ 
    // process data and assign to model 
    return View(model); 
} 
else 
{ 
    Response.StatusCode = (int) System.Net.HttpStatusCode.NotFound; 
    return View("NoSectionFound"); 
} 

注意,通过返回不同的看法,不必返回不同的页面。 URL仍然是相同的,因为它基于控制器而不是视图。通过渲染不同的视图,您不必在通常显示数据的视图中使代码复杂化。

顺便说一句,避免给“太多”的信息,如“无效的部分ID”,它可以引导潜在的攻击者到哪里“戳”下一步。

我也会重新排列代码,所以只有在找到section的时候才给model赋值,而对于“no section found”视图,你显然不需要传递任何模型。

+0

不同意。当你在imgur,facebook等网站上找到一个无效的ID时,你会得到一个'404 Not Found'。它非常常见,非技术人员可以理解404。如果担心用于损害系统的ID,可以使用UUID/GUID等非序列ID。 – Aron

+0

你有什么不同意? – Floremin

+0

控制器中的检查在控制器级别处理它,但仍然在视图级别上收到空引用错误,并且正如原始文章中所述,我想将错误消息返回给视图。我的贴子代码中的错误信息现在只是基本的东西,为了让事情正常运行,然后从那里长大 – Matthew