2010-05-19 64 views
1

我想通过使用MVC的部分视图中的表单上载图像。MVC:在局部视图中上传图像,路由问题

查看代码:

<form action="/Item/ImageUpload" method="post" enctype="multipart/form-data"> 
    <%= Html.TextBox("ItemId",Model.ItemId) %> 
    <input type="file" name="file" id="file" /> 
    <input type="submit" value="Add" /> 
</form> 

操作代码:

public void ImageUpload(string ItemId, HttpPostedFileBase file) 
{ 
// upload image 
// Add Image record to database 
// Associate Image record to Item record 
//Go back to existing view where the partial view sits 
RedirectToAction("Details/"+ItemId); 
} 

将图像上传成功

所有数据操作是否按预期工作

的但是不是重定向查看“Item/Details/id”,页面进入“/ Item/ImageUpload”

我尝试了几种不同的方法来做到这一点,包括使用jsonResultAction,但都失败了在这个相同的结果。

我在哪里做错了,有什么想法?在此先感谢

编辑:

我才意识到所有这些都是由代码呈现不正常的路由。

例如:[ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ] ,而不是渲染<a href="/Account/LogOff">Home</a>它呈现其被硬编码都是打工<a href="">Home</a>

但是所有的路线。

我在哪里打破它?它麻烦,我不能恢复它。

代码的Global.asax.cs

public class MvcApplication : System.Web.HttpApplication 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 


      //routes.MapRoute(
      // "ImageResize", 
      // "{controller}/{action}/{id}/{value}", 
      // new { controller = "Image", action = "ShowImage", id = "", value = "" }); 

      routes.MapRoute(
       "Default",            // Route name 
       "{controller}/{action}/{id}/",       // URL with parameters 
       new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
      ); 


     } 

     protected void Application_Start() 
     { 
      RegisterRoutes(RouteTable.Routes); 
     } 


    } 

回答

1

我认为你需要使用return RedirectToAction("Deati....

如果你已经有了关键字return有然后添加到您的问题,我会删除这个答案

编辑

在这种情况下,我会写一个路线WH ICH允许你再这样说:return RedirectToRoute("name", model);

EDIT 2

我只能在这一点上我觉得是从账户控制器的自动生成报价。

public ActionResult LogOff() 
{ 

    FormsAuth.SignOut(); 

    return RedirectToAction("Index", "Home"); 
} 

望着你提出的代码,我看到public void ImageUpload而非public ActionResult ImageUpload

+0

我也尝试过,既没有工作也没有工作 – 2010-05-19 04:19:40

+0

我刚刚尝试通过返回ActionResult,但页仍然去“Item/ImageUpload”与“路由表中没有路由匹配提供的值”错误,因为我做了不要在我的项目中创建这个视图。 – 2010-05-19 04:24:32

0
RedirectToAction("Details/"+ItemId); 

这应该是

return RedirectToAction("Details", new {Id = ItemId}); 

你不只是添加东西像那样RedirectToAction。 它使用反射来拾取您重定向到的方法的参数。