Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public ActionResult SenddOTP()
       {
           string numbers = "0123456789";
           Random objrandom = new Random();
           string strrandom = string.Empty;
           for (int i = 0; i < 5; i++)
           {
               int temp = objrandom.Next(0, numbers.Length);
               strrandom += temp;
           }
           ViewBag.otp = strrandom;

           return RedirectToAction("SendEmailtoUser");
           //  return View("SendOTP");
       }

       public JsonResult SendEmailtoUser()
       {

           bool result = false;
           var OTP = ViewBag.otp;
           result = SendEmail("user@gmail.com", "Hello", OTP);
           return Json(result, JsonRequestBehavior.AllowGet);
       }


What I have tried:

i have tried many things but nothing works
Posted
Updated 22-Aug-19 3:10am

If i understand you correctly you want to pass data via RedirectToAction method. If yes, then...
Controller.RedirectToAction Method (System.Web.Mvc) | Microsoft Docs[^] has several overloads.

If you want to return result from other action, you can use something like this:
C#
return RedirectToAction( "Main", new RouteValueDictionary( 
    new { controller = controllerName, action = "Main", Id = Id } ) );
 
Share this answer
 
Comments
Member 14552976 19-Aug-19 6:49am    
i want to use the result of SendOTP() method in SendEmailtoUser() method
Maciej Los 19-Aug-19 6:49am    
So, change the code to your needs!
Member 14552976 19-Aug-19 7:28am    
how.i am new mvc
Maciej Los 19-Aug-19 8:32am    
And what you expect me to do?
Member 14552976 20-Aug-19 1:48am    
help me out?
RedirectToAction returns a response to the client telling it to make a new request to the specified action.

The ViewBag contents are not persisted between requests. Anything you add to the ViewBag before returning a redirection response will not be available when the browser makes the new request.

There are a couple of options here:

1) Don't use RedirectToAction:
C#
public ActionResult SenddOTP()
{
    ...
    ViewBag.otp = strrandom;
    return SendEmailtoUser();
}

private JsonResult SendEmailtoUser()
{
    string OTP = ViewBag.otp;
    bool result = SendEmail("user@gmail.com", "Hello", OTP);
    return Json(result, JsonRequestBehavior.AllowGet);
}
Or:
C#
public ActionResult SenddOTP()
{
    ...
    return SendEmailtoUser(strrandom);
}

private JsonResult SendEmailtoUser(string OTP)
{
    bool result = SendEmail("user@gmail.com", "Hello", OTP);
    return Json(result, JsonRequestBehavior.AllowGet);
}
The browser will make a single HTTP request, and there will be no redirection involved.

2) Use the TempData storage:
C#
public ActionResult SenddOTP()
{
    ...
    TempData["otp"] = strrandom;
    return RedirectToAction(nameof(SendEmailtoUser));
}

public JsonResult SendEmailtoUser()
{
    string OTP = TempData["otp"] as string;
    bool result = SendEmail("user@gmail.com", "Hello", OTP);
    return Json(result, JsonRequestBehavior.AllowGet);
}
This will still use two HTTP requests.

NB: In this case, you would need to validate that the TempData value existed, since the user could navigate directly to the SendEmailToUser action without going via the SenddOTP action.

NB2: There's only one "d" in "Send". :)
 
Share this answer
 
Comments
Member 14552976 23-Aug-19 3:02am    
thanks for your reply.
Maciej Los 25-Aug-19 13:45pm    
Well explained!
EhabAhmed 31-Aug-19 23:12pm    
Thanks

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



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