12

I am developing an application in MVC3 and I am stuck at one place. Every time when control goes to IIndex1 action its argument value has become 0. But it should be same as value in IIndex action argument. I have used session, ViewBag, ViewData but my problem remains. Please suggest me.

public ActionResult GetMDN(string msisdn)
{
    number = msisdn.Substring(0, msisdn.IndexOf('$'));
    if (number.ToLower() != "unknown" && number.Length == 12)
    {
        number = number.Remove(0, 2);
    }
    Session["msdresponse"] = number;
    Session["moptr"] = msisdn.Substring(msisdn.LastIndexOf('$') + 1);
    number = msisdn;
    int sngid=int.Parse(ViewData["isongid"].ToString());
    return RedirectToAction("IIndex1", new { iid = sngid });
}

public ActionResult IIndex(int id)
{
    ViewBag.isongid = id;
    ViewData["isongid"] = id;
    Response.Redirect("http:XXXXXXXXXXXXXXXX");
    return RedirectToAction("GetMDN");
}

public ActionResult IIndex1(int iid)
{

}
2
  • is this code not working for you? Commented May 15, 2014 at 12:02
  • No..... Thats why I have posted here. Main issue is that when control pass from GetMDN action to IIndex1 Action, argument of Int iid hase became 0. But it should be same as whatever value have in IIndex action argument Commented May 15, 2014 at 12:05

3 Answers 3

32

You can use TempData. You can pass any type of data between two actions, whether they are in same controller or not. Your code should be something like it:

    public ActionResult GetMDN(string msisdn)
    {
        int sngid=10;

        TempData["ID"] = sngid;

        return RedirectToAction("IIndex");
    }

    public ActionResult IIndex()
    {
        int id = Convert.ToInt32(TempData["ID"]); // id will be 10;
    }
Sign up to request clarification or add additional context in comments.

Comments

4

Use TempData instead of ViewData/ViewBag to store data that should persist after redirect. ViewData/ViewBag allow to pass value from controller to view.

Something to read on this subject:

http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-cplusViewBagplusandplusTem

http://msdn.microsoft.com/en-us/library/dd394711(v=vs.100).aspx

Comments

0

you can use TempData["name"] = variableToPass;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.