views:

51

answers:

3

I am stuck with redirecting problem in ASP.NET MVC project. I have mapped tables via LINQtoSQL and each has unique ID as primary key.

I am implementing functionallity of 'CREATE'. Basically, after new value is added into SQL table (which means I pressed Save button), I want to be redirected to Details of this freshly added item.

Here's little code how I am doing it :

[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Create(Item item) {
....
return RedirectToAction("Details", new { id = item.ItemID });

Trouble is, I am never redirected to Details view (I have Details.aspx view for items).

When I check CallHierarchy in Visual Studio (2010 pro) the hierarchy is indeed little strange, like this :

RedirectToAction(string,object)

  • Calls To 'RedirectToAction'
    • Create
      • Calls To Create (no results)
      • Calls From Create (methods of created instance. From there I'll get back to 'RedirectToAction' and to 'Calls to Create' and 'Calls From Create' etc. etc. - loop
    • Edit
  • Calls From 'RedirectToAction'
    • Not supported

I am looking for some tools or more specifically 'know how' (since VS probably has some tools) to debug this kind of situations.

PS: rooting is default :"{controller}/{action}/{id}",

Thanks

+1  A: 

Checck your routes with Phil Haack's Route Debugger. Make sure that the correct route is being used, and the correct controller method is being called.

Robert Harvey
I'll give it a try, thanks
Xorty
Unfortunatelly, it doesn't seem to work for me. I placed RouteDebug.dll into bin folder, but VS won't know RouteDebug class ...
Xorty
You have to add RouteDebug.DLL to your references in Visual Studio, and then follow Phil Haack's directions for patching it into global.asax.
Robert Harvey
Thanks, it's working now. However, pressing "Save" button can't be tested via URL.
Xorty
Yes, I just realized that. You should try calling the "details" controller method directly, using an appropriate URL in the browser. If that works, consider using `RedirectToRoute` instead of `RedirectToAction.` I find that I have better control using RedirectToRoute.
Robert Harvey
Further investigating gave me hint, that routing might be perfectly normal ...It seems more like LINQtoSQL problem atm, I'll report later (if I won't be already dead)
Xorty
Ok, definitelly not redirecting problem. However, you provided handy tool, so let's consider this as answered :)
Xorty
A: 

You would need in the same controller

public ActionResult Details(int id)
{
    return View();
}

in additon to the Details.aspx view.

Khnle
yep, I have ...
Xorty
Details are working, but I am not redirected after I create new Item
Xorty
A: 

Use the debugger to step to the line where RedirectToAction is called. Confirm that the line is realy hit. Press F5 to continue after it.

Once the code is executed check in Firebug under network if a 302 is emitted. See what is in the details for the request.

If no 302 is emitted I would try return RedirectToAction("Index") just to know if the call to details is wrong or there is another error.

Malcolm Frexner