MVC4 - Ajax.ActionLink()GET返回500内部服务器错误
当我单击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>
它也可能是有用的知道,在另一个谐音页面工作如预期,所以我不认为这是缺少脚本的问题。我不知道为什么会发生这种情况 - 一个解决方案将非常感谢。
我已经解决了这个问题。我回来了currentTenant.ReferencePhotos.ToList()
扔了ArgumentNullException
。现在我只返回currentTenant
,GET按预期工作。
很高兴它是固定的,但有两件事。你不应该检查'currentTenant.ReferencePhotos == null' - 标准是在这个对象构造函数中'new'这个列表(或任何列表)。其次,你应该为你的网络应用启用elmah;这将记录任何500错误,并且很可能会使这个错误非常明显(您不应该从浏览器端调试这些错误) – wal 2013-03-23 16:13:58
感谢您的建议。 ReferencePhotos是一个ICollecetion(我首先使用代码),它可以在对象构造函数中以与列表相同的方式实例化吗?其实我之前读过elmah,感谢提醒我,我会确保启用它。 – MattSull 2013-03-23 16:20:38
是的,如果它的'ICollection',在你的ctor中,你可以简单地去'ReferencePhotos = new List
我发现有趣的是,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