C# Ajax.ActionLink未找到404错误

C# Ajax.ActionLink未找到404错误,c#,ajax,asp.net-mvc-4,http-status-code-404,C#,Ajax,Asp.net Mvc 4,Http Status Code 404,我有一个部分视图,它有一个Ajax.ActionLink,当单击它时,应该用另一个允许用户上传图像的部分视图替换视图(target DIV)。我在页面上有几个这样的功能,我只是不明白为什么我在这个特殊的功能上一直得到404 以下是我所拥有的: MyProfile.cshtml保存所有部分(租户参考照片为更新目标DIV): 以下ActionResult是在Ajax.ActionLink中没有调用的,并给出了404错误: [HttpPost] public ActionResult UploadRe

我有一个部分视图,它有一个
Ajax.ActionLink
,当单击它时,应该用另一个允许用户上传图像的部分视图替换视图(target DIV)。我在页面上有几个这样的功能,我只是不明白为什么我在这个特殊的功能上一直得到404

以下是我所拥有的:

MyProfile.cshtml保存所有部分(租户参考照片为更新目标DIV):

以下
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();
    }
}
[HttpPost]
public ActionResult UploadReference(HttpPostedFileBase文件,租户)
{
如果(文件!=null)
{
如果(file.ContentLength>10240)
{
AddModelError(“文件”,“文件大小不应超过10 KB”);
返回视图();
}
var-supportedTypes=new[]{“jpg”、“jpeg”、“png”、“jpg”、“jpeg”、“png”};
var fileExt=System.IO.Path.GetExtension(file.FileName).Substring(1);
如果(!supportedTypes.Contains(fileExt))
{
AddModelError(“照片”,“无效类型。仅支持以下类型(jpg、jpeg、png)”);
返回视图();
}
使用(var db=new LetLordContext())
{
var reference=db.Image.Create();
//将HttpPostedFileBase转换为字节数组
MemoryStream target=新的MemoryStream();
file.InputStream.CopyTo(目标);
字节[]photo=target.ToArray();
reference.File=photo;
reference.Format=fileExt;
reference.dateupload=DateTime.Now.Date;
参考文献.说明=”;
reference.Name=“”;
db.Image.Add(参考);
db.SaveChanges();
返回PartialView(“\u tenantreferencephotosparial”,文件);
}
}
其他的
{
返回视图();
}
}
为完整起见,以下是可以上载图像的局部视图:

<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>

@使用(Ajax.BeginForm(“UploadReference”、“租户”、FormMethod.Post、,
新选择
{
InsertionMode=InsertionMode.Replace,
HttpMethod=“POST”,
UpdateTargetId=“租户参考照片”
}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
选择一个文件:
}
MyProfile.cshtml中的所有脚本都被引用。unobtrusive-ajax.js是否需要作为脚本包含在所有局部视图中,即使它在Layout.cshtml和/或MyProfile.cshmtml中被引用


有人能发现我出现上述错误的原因吗?

在您的代码中
UploadReference
HttpPost
属性修饰,因此只有在发布时才可以访问它。在您的视图中,您已将
HttpMethod
配置为获取。当您将其更改为
POST
时,它应该可以工作:

@Ajax.ActionLink("Upload now?",
    "UploadReference",
    new AjaxOptions
    {
        UpdateTargetId = "tenant-reference-photos",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        LoadingElementId = "ajax-loader"
    })

让我的代码保持原样,我想我应该有一个GET ActionResult,返回partial _TenantUploadReferencePartial?听起来正确吗?是的,您是正确的-您可以在视图中删除
HttpPost
属性,而不是更改
HttpMethod
。但在我看来,你应该把两种情况分开。当您只需要返回视图并且它与GET一起工作时的第一个操作。支持上传逻辑的帖子的第二个操作。我已经按照上面所述做了,但是现在我得到了一个500错误。当我在调试模式下单步执行GetActionResult时,它会返回PartialView,但实际上它并没有更新目标DIV,并且在客户机中得到500错误。我会继续调查的。
[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>
@Ajax.ActionLink("Upload now?",
    "UploadReference",
    new AjaxOptions
    {
        UpdateTargetId = "tenant-reference-photos",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        LoadingElementId = "ajax-loader"
    })