Pixel Parker
Pixel Parker

Reputation: 3

Redirect from view to different view in asp.net mvc

I have a problem transfering data from one view to another via the controler actions. I the first view is a grid.mvc-Grid displayed. By select on row of the grid I get the ID for that object. by transfering this to an action in the controler I try to filter the data. That works fine.

Here is the filter:

[HttpGet]
public ActionResult PersonenById(int id)
{
    var personen = new ObservableCollection<Person>();
    //Getting the data here :-)

    foreach (DataRow r in access.Rows)
    {
        Person p = new Person();
        //do some stuff 
        personen.Add(p);
    }

    //return PartialView("Personen", personen); //does not work
    TempData["personen"] = personen;
    return RedirectToAction("Personen"); // redirect to another view
}

In method II the view is filled:

public ActionResult Personen()
{
    var persons = new ObservableCollection<Person>();
    if (TempData["Persons"] == null)
    {

     }
  return View(persons); //Works perfect
}   
else
{
    persons = (ObservableCollection<Person>) TempData["Persons"];
    return View(persons);//does not redirect to that View
}

}

(Sorry for the strange formating. :-))

Is there any different way to send data from a view to another? I tried: return partial; return View("Persons",persons); and a lot other stuff.

Upvotes: 0

Views: 2036

Answers (2)

BJury
BJury

Reputation: 2614

You can redirect in a .cshtml view.

Eg:

    Context.Response.StatusCode = 403;
    Context.Response.Redirect(
        $"{Context.Request.PathBase}/Error/403", false);

Upvotes: 1

Andrei Olaru
Andrei Olaru

Reputation: 304

Should work like this:

return RedirectToAction("Personen", model);

Also, the "Personen" action should have the model as an argument, like this:

public ActionResult Personen(Person model) ...

LE: I have also noticed you have tried to send the data through the TempData object. Make sure the indexed object's name is the same (e.g. TempData["person"] everywhere)

Hope it answers your question.

Upvotes: 0

Related Questions