ASP.NET MVC Redirect with model

76,879

Solution 1

Straight from my own app:

public ActionResult Create(Booking item)
{
    if (ModelState.IsValid)
    {
        int newID = _tasks.Create(item);
        // NEW section to emulate model being populated for use in Details view
        TempData["additionalData"] = "Person created successfully";
        return RedirectToAction("Details", new { id = newID });
    }
    else
    {
        return View();
    }
}

then, couldn't your "Details" action be like this:

public ActionResult Details(int id)
{
    var item = _tasks.GetByKey(id);
    var additionalData = TempData["additionalData"];
    if(item != null) {
        if(additonalMessage!=null)
        {
            item.additionalData = additionalData;
        }
        return View(item);
    }
    else
        return View("Notfound");
}

couldn't you adopt a similar approach??

You could just do the redirect as per convention and have a flag set (on tempdata as above) that gives this message? The tempadata flag would ONLY be set inside the Create action, therefore would only ever happen on Creating a new 'person' object. thus the Details action would only ever show it as a consequence of the Create action

Solution 2

You could supplement what has been offered (use RedirectToAction and routing) with the use of TempData

[HttpPost]
public virtual ActionResult Create(IEnumerable<OrderItem> orderItems)
    {
        if (orderItems.Count() == 0)
        {
            return RedirectToAction("NoOrderItems");
        }
        else
        {
            TempData["orderItems"] = orderItems;
            return RedirectToAction("Confirm");
        }
    }

    [HttpGet]
    public virtual ActionResult Confirm()
    {
        var orderItems = TempData["orderItems"] as IEnumerable<OrderItem>;
        if (orderItems == null || orderItems.Count() == 0)
        {
            this.InvokeHttp404(ControllerContext.HttpContext);
        }

        return View(orderItems);
    }

I use this for items that I might not want to create again on subsequent requests or persist quite yet in the database yet. With this, I don't need null checks in my view, as the Confirm page can only be "gotten" if there is data for it.

Solution 3

This should take you to the Details Model, passing the ID with it.

return RedirectToAction("Details", new { id = person.PersonID });

Solution 4

Mind you could also take the type-safe approach of MvcContrib and do

return this.RedirectToAction<MyController>(c => c.Details(person.PersonID));
Share:
76,879
user460667
Author by

user460667

Updated on October 22, 2020

Comments

  • user460667
    user460667 over 3 years

    I currently have a method in my controller which accepts a form collection, saves the data, and then displays the data in a 'Details' page. At the moment, the code currently looks something like:

    [HttpPost]
    public ActionResult Create(PersonModel person)
    {
        if (person.IsValid())
        {
            person.additionalData = "Person created successfully";
            return View("Details", person);
        }
    }
    

    The problem is that returning the Details view in this manner retains the URL mysite/Person/Create - ideally I would like the URL to be mysite/Person/Details/{PersonId}.

    Im sure this must be possible. Obviously, I could use Response.Redirect, however this doesn't allow me to pass the model to the view. As far as I can tell, I can't pass the model using RedirectToAction?

    Thanks for the help.

    EDIT: To confirm - the model produced by the Create action is different to the default one created by the Details action. Therefore, doing a straight redirect to Action and passing the Id does not work as the model produced is not correct. To give more context, the model from the Create action contains an additional value saying 'Person created successfully', if I redirect to the Details action, this message is not present in the model.

  • user460667
    user460667 over 13 years
    Hi, the problem with this is that the default action does not populate the model with same data as if it come via the 'Create' action. Looking at other answers, the best solution seems to be TempData.
  • Faizan S.
    Faizan S. over 13 years
    You could create an InitializeModel method that does all the data assigning magic for you..
  • Charlino
    Charlino over 13 years
    This is the way to go. If a simple message in tempdata isn't enough, you can just throw the whole PersonModel object into tempdata and get it back from tempdata in your Details action method.
  • Michael Bray
    Michael Bray about 7 years
    And if you don't want the full power of MvcContrib, you can use the MvcNavigationHelpers at github.com/uglybugger/MvcNavigationHelpers
  • Abdul Hannan
    Abdul Hannan almost 7 years
    great code I havent tested it yet but soon have to work on such component, I have a question, what if I refresh the page is it gonna work or the tempdata will get expired