7
Answers

mvc-how to Pass values from controller to controller?

How to select particular value from table and store it in viewbag.And also how to use that stored values in different controller in mvc5? 
Answers (7)
0
Ranjit Powar

Ranjit Powar

NA 8.1k 496.6k 7y
To pass values from one action method to another use Tempdata. Other way is store information in session.
Accepted
0
Akshaya arumugam

Akshaya arumugam

NA 57 2.3k 7y
Thank you Chetan Ranpariya
0
Chetan Ranpariya

Chetan Ranpariya

NA 1.6k 234 7y
TempData is used when you are redirecting from one controller action to another controller action. In your code there is no redirection happening. Both the Controller actions are returning views.
 
If I am correct, you want to store the PatientCode selected in Controller1 somewhere so that you can use that is Controller2.
 
To be able to do this I think you should use Session variable. You can store "PatiendCode" in Session from Controller1 and retreive it in Controller 2. 
 
  1. //Write following in Controller1  
  2. Session["CurrentPatient"] = PatientCode;
  3.   
  4. //Write following in Controller2  
  5. string patCode = Session["CurrentPatient"].ToString();  
 
0
Akshaya arumugam

Akshaya arumugam

NA 57 2.3k 7y
This is my code.But it takes null value to the second contoller
Controller1: 
public ActionResult ExaminePatient(string PatientCode)
{
List<Models.ExaminePatient> itemList = new List<Models.ExaminePatient>();
ExaminePatient examine = new ExaminePatient();
ExamineVm vm = new ExamineVm();
ExamineLoadingValues drop = new ExamineLoadingValues();
vm.Status = drop.Statusload();
itemList = drop.Visitinfo(PatientCode);
ViewData["itemList"] = itemList;
if(PatientCode!=null)
{
patcode = PatientCode.ToString();
ViewBag.patcode = patcode;
TempData["patcode"] = patcode;
}
return View("ExaminePatient", vm);
}
 
Controller2:
public ActionResult TestSelection()
{
List<Models.TestOrdereds> itemList = new List<Models.TestOrdereds>();
TestOrderDbs test = new TestOrderDbs();
TestOrderViewmodel vm = new TestOrderViewmodel();
itemList = test.Testordervalues();
ViewData["itemList"] = itemList;
TempData["patcode"] = patcode;
return View("TestSelection", vm);
}
 
 
0
Ibrahim Siddique

Ibrahim Siddique

NA 950 5.5k 7y
Hi Akshaya, 
 
Hope you are doing well 
 
If your passing Value from one controller to another controller directly the you can use TempData it will be an best soution for you.     
0
Akshaya arumugam

Akshaya arumugam

NA 57 2.3k 7y
how to use it.Can you show me an example?
0
Chetan Ranpariya

Chetan Ranpariya

NA 1.6k 234 7y
What code you have tried for this so far? Can you give some example about what exactly do you want?