[英]MVC4 - Ajax.ActionLink() GET returning 500 internal server error
I'm getting a 500 internal server error when I click on an Ajax.ActionLink
. 单击
Ajax.ActionLink
时出现500内部服务器错误。 I have a profile page that's made up of a number of partials. 我有一个由多个部分组成的个人资料页面。 Each partial makes Ajax calls to the server to allow for editing of user information related to that specific partial.
每个部分都对服务器进行Ajax调用,以允许编辑与该特定部分相关的用户信息。
I've implemented 3 partials with said functionality that work fine. 我已经实现了3个部分功能,并且功能正常。 The one I'm working on now should initially show a list of uploaded images - if the user hasn't uploaded any images the
Ajax.ActionLink
mentioned previously will be shown, which when clicked will bring the user to a partial that facilitates image uploading. 我现在正在处理的那个应该首先显示上传图像的列表 - 如果用户没有上传任何图像,将显示前面提到的
Ajax.ActionLink
,点击后将使用户进入部分,便于图像上传。
Here's what I'm seeing in Chrome when I hit the link: 这是我点击链接时在Chrome中看到的内容:
Here's the GET and POST ActionResults
: 这是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();
}
}
When I step through the debugger with a break point on the GET ActionResult
it hits return PartialView
and no exceptions are thrown. 当我通过GET
ActionResult
上的断点逐步调试调试器时,它会return PartialView
并且不会抛出任何异常。
In _TenantUploadReferencePartial
I use 在
_TenantUploadReferencePartial
我使用
@using (Ajax.BeginForm("TenantUploadReference", "Tenants", FormMethod.Post, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", UpdateTargetId = "tenant-reference-photos" }))
and in _TenantReferencePhotosPartial
(where the ActionLink
throws 500 error) I use this 并在
_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>
It may also be useful to know that other partials on the page work as expected, so I don't think it's an issue with missing scripts. 知道页面上的其他部分按预期工作也可能很有用,所以我认为缺少脚本是一个问题。 I'm at a loss as to why this is happening - a solution would be much appreciated.
我不知道为什么会这样 - 一个解决方案将非常感激。
I've solved the problem. 我已经解决了这个问题。 I was returning
currentTenant.ReferencePhotos.ToList()
which threw a ArgumentNullException
. 我正在返回
currentTenant.ReferencePhotos.ToList()
,它抛出了一个ArgumentNullException
。 Now I'm returning just currentTenant
, and the GET is working as expected. 现在我只返回
currentTenant
,GET正在按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.