當我單擊Ajax.ActionLink
時,出現500內部服務器錯誤。我有一個由多個部分組成的配置文件頁面。每個部分都會向服務器發出Ajax調用,以允許編輯與該特定部分相關的用戶信息。MVC4 - Ajax.ActionLink()GET返回500內部服務器錯誤
我已經實現了3個功能可以正常工作的partials。我現在正在使用的那個應該最初顯示上傳的圖像列表 - 如果用戶沒有上傳任何圖像,則會顯示前面提到的Ajax.ActionLink
,點擊該圖像會使用戶看到部分圖像上傳。
這裏是我所看到的在Chrome時,我打的鏈接:
這裏的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>
它也可能是有用的知道,在另一個諧音頁面工作如預期,所以我不認爲這是缺少腳本的問題。我不知道爲什麼會發生這種情況 - 一個解決方案將非常感謝。
我發現有趣的是,GET操作永遠不會爲Model.ReferencePhotos返回null,但您仍然在視圖上測試null。視圖中的其他內容會發生什麼?你確定get方法不會簡單地嘗試/ catch並返回Model = null的View()嗎? – 2013-03-23 15:05:36
我在GET中得到一個NullReferenceException,並且在調查時發現,使用null合併運算符可以在這種情況下解決這個問題。在使用操作符之前,我甚至需要測試null值嗎?否則將顯示參考照片,如果用戶有任何,如果他們沒有(if)應用程序顯示ActionLink給用戶。爲了再次確認,我剛剛通過調試器完成了GET,模型不爲空,並返回PartialView。 – MattSull 2013-03-23 15:18:46
對不起,我剛剛意識到你的意思,這是有道理的 - 關於永不返回null,並仍然在視圖中進行測試。我在GET中使用運算符的原因是處理NullReferenceException。 – MattSull 2013-03-23 15:27:53