开发者

MVC: Upload image in partial view, routing problem

I am trying to upload images via a form which sits in partial view using MVC.

View Code:

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

Action Code:

public void ImageUpload(string ItemId, HttpPostedFileBase file)
{
// upload开发者_StackOverflow社区 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);
}

The Image is uploaded successful

All the data manipulation are working as expected

However instead of redirect to view "Item/Details/id", page went to "/Item/ImageUpload"

I tried several different way of doing this including using jsonResultAction, but all failed in this same result.

where did i do wrong, any ideas? thanks in advance

Edit:

I just realized all the routing which are render by the code is not working.

for example: [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ] instead of rendering <a href="/Account/LogOff">Home</a> it renders <a href="">Home</a>

however all the routings which were hard coded are all working.

Where did i break it?? It troubles that I cant event restore it.

Code for 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);
        }


    }


I think you need to use return RedirectToAction("Deati....

If you already have the keyword return there then add that to your question and I'll remove this answer

EDIT

In that case I'd write a route which allows you to then say something like return RedirectToRoute("name", model);

EDIT 2

All I can offer at this point I think is from the account controller that's auto generated.

public ActionResult LogOff()
{

    FormsAuth.SignOut();

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

Looking at the code you have presented I see public void ImageUpload rather than public ActionResult ImageUpload.


RedirectToAction("Details/"+ItemId);

This should Be

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

You dont just add stuff to the RedirectToAction like that. It uses reflection to pick up the Parameters for the method that you are redirecting to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜