简体   繁体   English

ASP.Net MVC中的RedirectToAction

[英]RedirectToAction in ASP.Net MVC

I'm using ASP.net MVC 4 我正在使用ASP.net MVC 4
I have a controller following: 我有以下控制器:

public class PersonController : Controller
{
    public ActionResult Edit(int id)
    {
        //todo something.........
    }

    [HttpPost]
    public ActionResult Edit(PersonModel model)
    {
        //todo something.........
        return RedirectToAction("Edit", model.Id); // => Okay
    }

    public ActionResult Create()
    {
        //todo something............
    }

    [HttpPost]
    public ActionResult Create(PersonModel model)
    {
        //todo something........

        return RedirectToAction("Edit", new { id = model.Id } ); //=>Okay
        //return RedirectToAction("Edit", model.Id); // => exception???, but Edit Action is Okay**
    }
}

I think you got the exception because the RedirectToAction Method has 6 signatures and when you write RedirectToAction("Edit", model.Id); 我认为您有例外,因为RedirectToAction方法具有6个签名,并且在您编写RedirectToAction("Edit", model.Id); it can be considered as a string and will search for a controller with the name model.Id . 它可以视为字符串,并将搜索名称为model.Id的控制器。 Have a look here MSDN:RedirectToAction . 在这里看看MSDN:RedirectToAction Hope it will help you 希望对您有帮助

By default MVC supports 'ID' andnot 'id' or any other: 默认情况下,MVC支持'ID'而不支持'id'或其他任何形式:

In your code :: 在您的代码中::

public ActionResult Edit(int id)
{
    //todo something.........
}

here you have declared int id because of that :: 在这里,您已经声明了int id因为::

return RedirectToAction("Edit", model.Id);

this is not working. 这是行不通的。

If you have been declared your Edit action as:: 如果已将您的“编辑”操作声明为::

public ActionResult Edit(int ID)
{
    //todo something.........
}

then you can use this:: 那么你可以使用这个:

return RedirectToAction("Edit", new { @Id = model.Id } );

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

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