Ajax.ActionLink
をクリックすると500の内部サーバエラーが発生します。私は、いくつかの部分的なものからなるプロフィールページを持っています。各部分は、Ajaxをサーバーに呼び出して、その特定の部分に関連するユーザー情報の編集を可能にします。MVC4 - Ajax.ActionLink()GET内部サーバエラー50030を返す
私は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();
}
}
私は休憩でデバッガをステップGET ActionResult
上のポイントはreturn PartialView
に達し、例外はスローされません。私は
@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>
これは知って使用使用_TenantUploadReferencePartial
で
その上の他のパーシャルページの作業が予想どおりであるため、スクリプトが不足しているとは思えません。私はなぜこれが起こっているのかを迷っています。解決策が大いに評価されるでしょう。
私はあなたのGETアクションがModel.ReferencePhotosにnullを返すことはないと考えていますが、ビューではまだヌルをテストしています。ビューのelseではどうなりますか?そして、getメソッドが単にModel/nullでView()をキャッチして返してもいないことは確かですか? –
私はGETでNullReferenceExceptionを取得していました。調査すると、null合体演算子を使用してこのタイプの状況を修正することができます。演算子を使用する前にnullをテストする必要がありますか? elseには参照写真が表示され、ユーザーにアクションがある場合はifが表示され、ユーザーにはActionLinkが表示されます。再確認するために、私はデバッガでGETを実行しました。モデルはnullではなく、戻ります。PartialViewはヒットしています。 – MattSull
申し訳ありません私はあなたが意味していたことを理解しました。それは意味があります - ヌルを返すことは決してありません。私がGETで演算子を使用していたのは、NullReferenceExceptionを処理するためでした。 – MattSull