Mat*_*ull 6 c# jquery asp.net-mvc-4
当我点击一个时,我收到500内部服务器错误Ajax.ActionLink
.我有一个由多个部分组成的个人资料页面.每个部分都对服务器进行Ajax调用,以允许编辑与该特定部分相关的用户信息.
我已经实现了3个部分功能,并且功能正常.我现在正在处理的那个应该首先显示上传图像的列表 - 如果用户没有上传任何图像,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();
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过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>
Run Code Online (Sandbox Code Playgroud)
知道页面上的其他部分按预期工作也可能很有用,所以我认为缺少脚本是一个问题.我不知道为什么会这样 - 一个解决方案将非常感激.
我已经解决了这个问题。我正在返回currentTenant.ReferencePhotos.ToList()
,抛出了一个ArgumentNullException
. 现在我刚刚返回currentTenant
,并且 GET 正在按预期工作。
归档时间: |
|
查看次数: |
2140 次 |
最近记录: |