2013-03-20 111 views
0

我有一个局部视图,它有一个Ajax.ActionLink,点击时应该用另一个允许用户上传图像的局部视图替换视图(目标DIV)。我在页面上有几个这样的工作,我似乎无法弄清楚为什么我一直在为这个特定的404获得一个。Ajax.ActionLink提供404找不到错误

这是我有:

MyProfile.cshtml保存所有谐音(租户参考照片是更新的目标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中的脚本。即使在Layout.cshtml和/或MyProfile.cshmtml中引用了unobtrusive-ajax.js是否需要作为脚本包含在所有部分视图中?

任何人都可以找到我为什么会得到上述错误?

回答

2

在您的代码中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" 
    }) 
+0

离开我的代码,我认为我应该有一个GET的ActionResult返回部分_TenantUploadReferencePartial?这听起来正确吗? – MattSull 2013-03-20 19:11:04

+0

是的,你有权利 - 而不是改变你的视图中的'HttpMethod',你可以删除'HttpPost'属性。但在我看来,你应该分开两个案例。当您只想返回视图并且它与GET协同工作时的第一个动作。支持上传逻辑的POST第二项操作。 – 2013-03-20 19:27:34

+0

我已经完成了上述操作,但现在我收到了500错误。当我在调试模式下遍历GET ActionResult时,它会返回PartialView,但它实际上并未更新目标DIV,并且在客户端中收到500错误。我会继续研究它。 – MattSull 2013-03-20 19:37:34