2013-03-21 44 views
6

當我單擊Ajax.ActionLink時,出現500內部服務器錯誤。我有一個由多個部分組成的配置文件頁面。每個部分都會向服務器發出Ajax調用,以允許編輯與該特定部分相關的用戶信息。MVC4 - Ajax.ActionLink()GET返回500內部服務器錯誤

我已經實現了3個功能可以正常工作的partials。我現在正在使用的那個應該最初顯示上傳的圖像列表 - 如果用戶沒有上傳任何圖像,則會顯示前面提到的Ajax.ActionLink,點擊該圖像會使用戶看到部分圖像上傳。

這裏是我所看到的在Chrome時,我打的鏈接:

enter image description here

這裏的GET和POST ActionResults

// 
    // GET: /Tenants/TenantUploadReference 

    [HttpGet] 
    public ActionResult TenantUploadReference() 
    { 
     try 
     { 
      var currentTenant = tenantRepository.GetLoggedInTenant(); 
      if (currentTenant.ReferencePhotos == null) 
      { 
       currentTenant.ReferencePhotos = currentTenant.ReferencePhotos ?? new List<ReferencePhoto>(); 
      } 
      return PartialView("_TenantUploadReferencePartial", currentTenant.ReferencePhotos.ToList()); 
     } 
     catch (Exception e) 
     { 
      ModelState.AddModelError("", e); 
      return View(); 
     } 
    } 

    // 
    // POST: /Tenants/TenantUploadReference 

    [HttpPost] 
    public ActionResult TenantUploadReference(HttpPostedFileBase file, Tenant tenant) 
    { 
     try 
     { 
      if (file != null) 
      { 
       if (file.ContentLength > 10240) 
       { 
        ModelState.AddModelError("file", "The size of the file should not exceed 10 KB"); 
        return View(); 
       } 

       var supportedTypes = new[] { "jpg", "jpeg", "png", "JPG", "JPEG", "PNG" }; 
       var fileExt = System.IO.Path.GetExtension(file.FileName).Substring(1); 

       if (!supportedTypes.Contains(fileExt)) 
       { 
        ModelState.AddModelError("photo", "Invalid type. Only the following types (jpg, jpeg, png) are supported."); 
        return View(); 
       } 

       using (var db = new LetLordContext()) 
       { 
        var reference = db.Image.Create<ReferencePhoto>(); 

        // Convert HttpPostedFileBase to byte array 
        MemoryStream target = new MemoryStream(); 
        file.InputStream.CopyTo(target); 
        byte[] photo = target.ToArray(); 

        reference.File = photo; 
        reference.Format = fileExt; 
        reference.DateUploaded = DateTime.Now.Date; 
        reference.Description = ""; 
        reference.Name = ""; 

        db.Image.Add(reference); 
        db.SaveChanges(); 

        return PartialView("_TenantReferencePhotosPartial", file); 
       } 

      } 
      else 
      { 
       return View(); 
      } 
     } 
     catch (Exception e) 
     { 
      ModelState.AddModelError("", e); 
      return View(); 
     } 
    } 

當我通過調試步驟有突破指向GET ActionResult,它會遇到return PartialView,並且不會引發異常。

_TenantUploadReferencePartial我用

@using (Ajax.BeginForm("TenantUploadReference", "Tenants", FormMethod.Post, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", UpdateTargetId = "tenant-reference-photos" }))

,並在_TenantReferencePhotosPartial(其中ActionLink拋出500錯誤),我用這個

@if (Model.ReferencePhotos == null) 
{ 
    <h3>You haven't uploaded any references! 
     @Ajax.ActionLink("Upload now?", 
      "TenantUploadReference", 
      new AjaxOptions 
      { 
       UpdateTargetId = "tenant-reference-photos", 
       InsertionMode = InsertionMode.Replace, 
       HttpMethod = "GET", 
       LoadingElementId = "ajax-loader" 
      })</h3> 

它也可能是有用的知道,在另一個諧音頁面工作如預期,所以我不認爲這是缺少腳本的問題。我不知道爲什麼會發生這種情況 - 一個解決方案將非常感謝。

+0

我發現有趣的是,GET操作永遠不會爲Model.ReferencePhotos返回null,但您仍然在視圖上測試null。視圖中的其他內容會發生什麼?你確定get方法不會簡單地嘗試/ catch並返回Model = null的View()嗎? – 2013-03-23 15:05:36

+0

我在GET中得到一個NullReferenceException,並且在調查時發現,使用null合併運算符可以在這種情況下解決這個問題。在使用操作符之前,我甚至需要測試null值嗎?否則將顯示參考照片,如果用戶有任何,如果他們沒有(if)應用程序顯示ActionLink給用戶。爲了再次確認,我剛剛通過調試器完成了GET,模型不爲空,並返回PartialView。 – MattSull 2013-03-23 15:18:46

+0

對不起,我剛剛意識到你的意思,這是有道理的 - 關於永不返回null,並仍然在視圖中進行測試。我在GET中使用運算符的原因是處理NullReferenceException。 – MattSull 2013-03-23 15:27:53

回答

4

我已經解決了這個問題。我回來了currentTenant.ReferencePhotos.ToList()扔了ArgumentNullException。現在我只返回currentTenant,GET按預期工作。

+1

很高興它是固定的,但有兩件事。你不應該檢查'currentTenant.ReferencePhotos == null' - 標準是在這個對象構造函數中'new'這個列表(或任何列表)。其次,你應該爲你的網絡應用啓用elmah;這將記錄任何500錯誤,並且很可能會使這個錯誤非常明顯(您不應該從瀏覽器端調試這些錯誤) – wal 2013-03-23 16:13:58

+0

感謝您的建議。 ReferencePhotos是一個ICollecetion(我首先使用代碼),它可以在對象構造函數中以與列表相同的方式實例化嗎?其實我之前讀過elmah,感謝提醒我,我會確保啓用它。 – MattSull 2013-03-23 16:20:38

+0

是的,如果它的'ICollection',在你的ctor中,你可以簡單地去'ReferencePhotos = new List ();'因爲List實現'ICollection' – wal 2013-03-23 16:34:08

相關問題