Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value from one action to another action having same controller

Hi I am developing an application in MVC3. and i am stuck at one place. Everytime 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 is 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)
        {

        }
like image 302
Abhay Singh Avatar asked May 15 '14 11:05

Abhay Singh


People also ask

What is used to send data from one action to another action?

TempData is used to pass data from one HTTP request to next HTTP request. o In other words, TempData is used to pass data from one controller to another controller or action to another action. TempData is a property of BaseController class.

Can we have two methods with same name in controller?

You can have two method with the same name (and in fact it's a good practice to do that for forms). There are two requirements to have similar named methods. The methods must have different parameters. This is dictated by the programming language and is basically method overloading.

How do you pass data between action methods in MVC?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form. This article talks about all of these ways, and illustrates them with code examples.


2 Answers

You can use TempData.You can pass every types of data between to action, 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;
    }
like image 110
Badhon Ashfaq Avatar answered Nov 21 '22 22:11

Badhon Ashfaq


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

like image 28
st4hoo Avatar answered Nov 21 '22 22:11

st4hoo