0

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.

3
  • "Sorry for the strange formating" - do not apologise, just format it correctly, because code is almost unreadable. Commented Jan 21, 2015 at 14:36
  • 1
    Your line endings aren't correct. Commented Jan 21, 2015 at 14:37
  • Formatted part of it but the second code block has too many braces so unsure what code is supposed to be. Also TempData["Persons"] != TempData["personen"] Commented Jan 21, 2015 at 14:39

2 Answers 2

1

You can redirect in a .cshtml view.

Eg:

    Context.Response.StatusCode = 403;
    Context.Response.Redirect(
        $"{Context.Request.PathBase}/Error/403", false);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

Thanks for the hint, but TempData["person"] is just a error caused by copy'n'paste. When I set a breakpoint to the targetview, the breakpoint will be reached in the view. But the view is not displayed.
You mean the .cshtml or .aspx view? There you must have the same ObservableCollection<Person> model specified, if yes.
Hi Andrei. Yes, its a .cshtml-View. The ObservableCollection<Person> is identical ( imo). Otherwise the Grid in the view wouldn'd get any data, or? What not happens is the redirect to the different view in browser. The view is not displayed.
You can't do a redirect with a payload. Redirects cause the client's browser to issue a GET request for the new URL. As a result, you can only pass data via a query string, since that's all the GET method supports. While you could attempt to break down your class into query string parameters, this would be clunky. If you need to work with the same object, it's best to just pass along the id with the new URL and look it up again with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.