开发者

ASP.NET MVC2: How to redirect to GET Details/5 after POST Add

I am absolute beginner in C#, ASP.NET and MVC2 and this means that I just might be missing something exceptionally basic. I tried to search开发者_开发百科 for it, but here again, I failed to come up with the proper incantations for neither Google not StackOverflow, so here comes the question:

I am trying to create a basic controller with two actions:

[HttpPost]
public ViewResult Create(CustomerCreateData data)
{
    CustomerRecord cr = //create customer record from input data...

    return RedirectToAction("Details");
}

public ViewResult Details(int id)
{
    CustomerRecord cr = // load customer record with specified id...
    return View(cr);
}

My idea is thet after successful POST /Customer/Create the user would be redirected to GET /Customer/Details/42 where 42 is id of the newly created customer record.

What is the proper incantation for this in ASP.NET MVC2

PS - I've seen countless examples of redirecting to "Index" action, but that is not quite enough.


You can pass data to the RedirectToAction method:

return RedirectToAction("Details", new { id = cr.Id });

This is assuming you either have a defined route, e.g. Customer/Details/{id} or that you still have the default route {controller}/{action}/{id}.


In Create ActionResult, after creation success make an action like this (or realy this for example):

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

This code produce a redirect to Details/id/{cr.Id}. Sorry for bad english (i'm italian)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜