How to pass List in Redirecttoaction

后端 未结 4 501
萌比男神i
萌比男神i 2020-12-01 23:36

I want to pass more then one parameter from RedirectToAction method

how can I pass?

My One Action Method

 [HttpPost, ActionN         


        
相关标签:
4条回答
  • 2020-12-02 00:16

    This is probably not even active anymore, but I'll leave how I did it here to maybe help someone else.

    I solved this using a simple Redirect instead of a RedirectToAction:

    List<int> myList = myListofItems;
    var list = HttpUtility.ParseQueryString("");
    myList.ForEach(x => list.Add("parameterList", x.ToString()));
    return Redirect("/MyPath?" + list);
    

    Then, on your other method:

    public ActionResult Action(List<int> parameterList){}
    
    0 讨论(0)
  • 2020-12-02 00:16

    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.

    0 讨论(0)
  • 2020-12-02 00:24

    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>;
    
    0 讨论(0)
  • 2020-12-02 00:36

    RedirectToAction method Returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

    You should either keep the data in a temporary storage like TempData / Session . TempData uses Session as the backing storage.

    If you want to keep it real Stateless, you should pass an id in the query string and Fetch the List of items in your GET Action. Truly Stateless.

    return RedirectToAction("Question", new { email = email,id=model.ID });
    

    and in your GET method

    public ActionResult Question(string email,int id)
    {
    
       List<QuestionClass.Tabelfields> fadd=repositary.GetTabelFieldsFromID(id);
        //Do whatever with this
       return View();
    }
    

    Assuming repositary.GetTabelFieldsFromID returns a List of TabelFields from the Id

    0 讨论(0)
提交回复
热议问题