简体   繁体   English

ASP.NET MVC2:如何在POST添加后重定向到GET Details / 5

[英]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. 我绝对是C#,ASP.NET和MVC2的初学者,这意味着我可能会缺少一些非常基础的知识。 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: 我试图搜索它,但在这里再次出现,我没有为Google而不是StackOverflow提出正确的咒语,因此出现了一个问题:

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. 我的想法是在成功POST /Customer/Create将用户重定向到GET /Customer/Details/42 ,其中42是新创建的客户记录的ID。

What is the proper incantation for this in ASP.NET MVC2 在ASP.NET MVC2中对此有什么适当的建议?

PS - I've seen countless examples of redirecting to "Index" action, but that is not quite enough. PS-我已经看到了无数重定向到"Index"操作的示例,但这还不够。

You can pass data to the RedirectToAction method: 您可以将数据传递给RedirectToAction方法:

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

This is assuming you either have a defined route, eg Customer/Details/{id} or that you still have the default route {controller}/{action}/{id} . 这是假设您具有已定义的路由,例如Customer/Details/{id}或仍具有默认路由{controller}/{action}/{id}

In Create ActionResult, after creation success make an action like this (or realy this for example): 在Create ActionResult中,创建成功后,请执行以下操作(或例如执行以下操作):

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

This code produce a redirect to Details/id/{cr.Id}. 此代码将重定向到Details / id / {cr.Id}。 Sorry for bad english (i'm italian) 对不起,英语不好(我是意大利语)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM