Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass List in Redirecttoaction

I want to pass more then one parameter from RedirectToAction method

how can I pass?

My One Action Method

 [HttpPost, ActionName("SelectQuestion")]
    public ActionResult SelectQuestion(string email,List<QuestionClass.Tabelfields> model)
    {

        List<QuestionClass.Tabelfields> fadd = new List<QuestionClass.Tabelfields>();
        for (int i = 0; i < model.Count; i++)
        {
            if (model[i].SelectedCheckbox == true)
            {
                List<QuestionClass.Tabelfields> f = new List<QuestionClass.Tabelfields>();
                fadd.Add(model[i]);
            }
        }

        return RedirectToAction("Question", new { email = email, model = fadd.ToList() });
    }

My another Action Method

    [HttpGet]
    public ActionResult Question(string email,List<QuestionClass.Tabelfields> model)
    {
    }

I am not getting values in model.

like image 948
Ajay Avatar asked Aug 24 '12 11:08

Ajay


People also ask

How do you pass an object in redirect to action?

You can not pass classes to RedirectToAction method, if you want to pass an entire object in a querystring or via POST you can serialize the object using XML or JSON and deserialize the object in the receiver controller. If you use this approach to be careful on the size of the object serialized.

How pass multiple parameters in RedirectToAction in MVC?

Second, to pass multiple parameters that the controller method expects, create a new instance of RouteValueDictionary and set the name/value pairs to pass to the method. Finally call RedirectToAction(), specifying the method name, controller name, and route values dictionary.

What is difference between redirect and RedirectToAction?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.


2 Answers

You cannot pass a collection of complex objects in urls when redirecting.

One possibility would be to use TempData:

TempData["list"] = fadd.ToList();
return RedirectToAction("Question", new { email = email});

and then inside the Question action:

var model = TempData["list"] as List<QuestionClass.Tablefields>;
like image 197
Pablo Claus Avatar answered Oct 19 '22 18:10

Pablo Claus


The way that I solved this problem was to serialize the list to a JSON object using the JsonConvert method from the Newtonsoft.Json nuget package. Then the serialized list can be passed as a parameter and then deserialized again to re-create the original list.

So in your SelectQuestion method you would use this code:

return RedirectToAction("Question", 
    new { 
        email = email, 
        serializedModel = JsonConvert.SerializeObject(fadd.ToList()) 
    });

And in your Question method, you would use this code to deserialize the object.

[HttpGet]
public ActionResult Question(string email, string serializedModel)
{
    // Deserialize your model back to a list again here.
    List<QuestionClass.Tabelfields> model = JsonConvert.DeserializeObject<List<QuestionClass.Tabelfields>>(serializedModel);
}

Important, this adds the model as a query string parameter to your url, so only do this with really simple small objects, otherwise your url will be too long.

like image 5
Tim Pickin Avatar answered Oct 19 '22 17:10

Tim Pickin