Ajax.ActionLink给出404未找到错误
本文关键字:错误 ActionLink 给出 Ajax | 更新日期: 2023-09-27 18:04:51
我有一个部分视图,有一个Ajax.ActionLink
,当点击应该替换视图(目标div)与允许用户上传图像的另一个部分视图。我的页面上有几个这样的工作,我只是不明白为什么我总是得到一个404为这个特定的一个。
我有:
MyProfile。cshtml保存所有的partial (tenant-reference-photos is update target DIV):
<div class="row-fluid">
<div class="span6 well-border" id="tenant-reference-photos">
@Html.Partial("~/Views/Tenants/_TenantReferencePhotosPartial.cshtml", Model)
</div>
<div class="span6 well-border" id="tenant-viewings">
@Html.Partial("~/Views/Tenants/_TenantViewingsPartial.cshtml", Model)
</div>
</div>
_TenantReferencePhotosPartial。cshtml (ActionLink给出404):
<div class="row-fluid">
@if (Model.ReferencePhotos == null)
{
<h3>You haven't uploaded any references!
@Ajax.ActionLink("Upload now?", // on click 404 returned in developer tools in Chrome
"UploadReference",
new AjaxOptions
{
UpdateTargetId = "tenant-reference-photos",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
LoadingElementId = "ajax-loader"
})
</h3>
}
</div>
下面的代码返回上述部分:
[HttpGet]
public ActionResult TenantReferencePhotos()
{
var currentTenant = tenantRepository.GetLoggedInTenant();
return PartialView("_TenantReferencePhotosPartial", currentTenant.ReferencePhotos.ToList());
}
下面的ActionResult
是在Ajax.ActionLink
中没有被调用的,并给出404错误:
[HttpPost]
public ActionResult UploadReference(HttpPostedFileBase file, Tenant tenant)
{
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();
}
}
为了完整起见,这里是可以上传图像的部分视图:
<div>
@using (Ajax.BeginForm("UploadReference", "Tenants", FormMethod.Post,
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "tenant-reference-photos"
}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
<div>
Select a file:
<input type="file" name="file" />
<input type="submit" value="UploadReference" />
</div>
}
</div>
MyProfile中的所有脚本。引用CSHTML。不引人注目的ajax.js需要作为一个脚本包括在所有的部分视图,即使它在布局中被引用。cshtml和/或myprofile . cshtml ?
谁能发现为什么我得到上面的错误?
在您的代码中,UploadReference
是用HttpPost
属性装饰的,因此只有在您进行POST时才能访问。在您的视图中,您已将HttpMethod
配置为GET。当您将其更改为POST
时,它应该工作:
@Ajax.ActionLink("Upload now?",
"UploadReference",
new AjaxOptions
{
UpdateTargetId = "tenant-reference-photos",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
LoadingElementId = "ajax-loader"
})