2014-01-09 94 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