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

后端 未结 2 929
余生分开走
余生分开走 2021-02-08 18:28

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

相关标签:
2条回答
  • 2021-02-08 19:00

    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

    0 讨论(0)
  • 2021-02-08 19:06

    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;
        }
    
    0 讨论(0)
提交回复
热议问题