Click here to Skip to main content
15,844,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ActionResult method for which I want to redirect to two other Action methods but this will depend on the value of a certain variable that is being passed in. When I attempt to run the action method it is not redirecting according to the logic but rather the else portion of the loop always.

C#
[HttpPost]
public ActionResult PushBatch(FormCollection formcollection)
{
     ......
     ......

    NewCodesCount();

    int count = NewCodesCount();

    if (count > 1)
    {
        return RedirectToAction("ActionRequired");
    }
    else
    {
        return RedirectToAction("BatchProcessing");
    }
}


When the code above runs it is always redirecting to the BatchProcessing action method even when the value of count is greater than 1.

What am I missing and why is the logic skipping the If part of the loop ?

What I have tried:

I have tried to use the variant below but then the code complains that my method now has no return value :

C#
[HttpPost]
public ActionResult PushBatch(FormCollection formcollection)
{
     ......
     ......

    NewCodesCount();

    int count = NewCodesCount();

    if (count > 1)
    {
        return RedirectToAction("ActionRequired");
    }
    else if (count < 1)
    {
        return RedirectToAction("BatchProcessing");
    }
}
Posted
Updated 3-Jun-21 6:35am
v2

1 solution

You are calling the NewCodesCount method twice, and ignoring the value returned from the first call. There's a good chance that the second call is returning either 1 or 0, but we can't tell you, because we can't see that method.

Try removing the first call to the method. If that doesn't fix it, you're going to need to debug your code to see why the NewCodesCount method is returning the wrong value.
 
Share this answer
 
Comments
Tshumore 4-Jun-21 6:48am    
I have since removed NewCodesCount and the result is still the same , the flow automatically redirects to else part even when its supposed to go to the URL specified in the If
Richard Deeming 4-Jun-21 9:54am    
So what happened when you debugged your code to find out why the NewCodesCount method isn't returning what you think it should be returning?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900