0

I am running into a strange problem when I needed to pass certain data from one function to the other in model:

public ActionResult TwoFA(string email, List<PhoneDataContract> phones, bool smsAuth, bool voiceAuth, string appName, string gaid)
{
    var model = new TwoFAModel(); <---Break Point

    model.EmailAddress = email;
    model.Phones = phones;
    model.SMSAuthentication = smsAuth;
    model.PhoneAuthentication = voiceAuth;

    return View("TwoFA", model);
}

public ActionResult Start(string method, string phone, string phone2, string phone3, string Email, string AppName, string Gaid)
{
    string[] PhoneList = new String[] { phone, phone2, phone3 };
    bool SMSAuth = false;
    bool VoiceAuth = false;

    var phoneDatas = new List<PhoneDataContract>();
    for (int i = 0; i < PhoneList.Length; i++)
    {
        PhoneDataContract phoneData = new PhoneDataContract();
        phoneData.Phone = PhoneList[i];
        phoneDatas.Add(phoneData);
    }

    var num = phoneDatas.Count;
    SMSAuth = true;
    VoiceAuth = true;

    return RedirectToAction("TwoFA", "TwoFA", new { email = Email, phones = phoneDatas, smsAuth = SMSAuth, voiceAuth = VoiceAuth, appName = AppName, gaid = Gaid });    <---- Break point
}

From view, I am passing certain data to the model in Start function. Then from there, I pass these data from Start to another function TwoFA. Every data is passed just fine, besides one thing: phoneDatas.

What I have tried: I put two break point and tried to trace through it. Before the first break point, I put a line var num = phoneDatas.count to make sure that the for loop ran as intended.And it did. Then At the first break point, I hover over the param, the data was there. Then when I step into my second break point and I hover over the param of TwoFA function, the list became empty.

Why? Everything else was there besides the List. I was not able to comprehend this as I am new to C series language in general, as well as ASP.Net. Is there something I am missing here?

Edit: The issue has to do with how RedirectToAction works. RedirectToAction only accept simple data structure like string or int. For array or List, you have to use tempData or serialize it first. Since I will flag this question down myself, for anyone who ran into this problem please see kapSir's comment.

ZpfSysn
  • 807
  • 2
  • 12
  • 30
  • 1
    Please read https://stackoverflow.com/questions/27684389/sending-a-list-using-redirecttoaction-in-mvc4 or https://stackoverflow.com/questions/12108770/how-to-pass-list-in-redirecttoaction or https://stackoverflow.com/questions/5753720/passing-info-to-another-action-using-redirecttoaction-mvc – kapsiR Mar 25 '19 at 14:02
  • Does your action name has the same name as your controller? Check the reference: https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2 – Luis Alves Mar 25 '19 at 14:04
  • @kapsiR thank you, should I close this question? – ZpfSysn Mar 25 '19 at 14:13
  • 1
    @LuisAlves yeah, it does. The problem actually has to do with how RedirectToAction works. – ZpfSysn Mar 25 '19 at 14:13
  • 1
    @aDev, yes I think you can close this. (You can edit your question with a hint to another and then close it) – kapsiR Mar 25 '19 at 15:03

0 Answers0