jimmit raval

jimmit raval

  • NA
  • 34
  • 481

I want to pass the selected value from controller to view

Nov 5 2018 4:40 AM
Hi,
 
I am very new to asp.net MVC and have my first project in hand.
 
I am having 2 pages Home and Attendance. 
 
In Home controller, i am getting 3 client names C1, C2, C3 from database in my dropdownlistfor.
 
View 
  1. @model WebApplication1.Models.ClientMdl  
  2.     @{  
  3.         ViewBag.Title = "Index";  
  4.     }  
  5.   
  6.     <h2>ATOM</h2>  
  7. @using (Html.BeginForm("Index""Home",FormMethod.Post,new { id="Frm1"}))  
  8. {  
  9.     <table>  
  10.         <tr></tr>  
  11.         <tr>  
  12.             <div>  
  13.                 Select Client name<br />  
  14.                 @Html.DropDownListFor(model => model.Client_Names, new SelectList(Model.Clients, "Client_Names""Client_Names"), "Select Client"new { onchange = "document.getElementById('Frm1').submit();" })  
  15.   
  16.                 @*                @Html.TextBoxFor(model => model.Client_Names)*@  
  17.                 @Html.ValidationMessageFor(model => model.Client_Names)  
  18.                 <input type="submit" value="Submit" />  
  19.   
  20.             </div>  
  21.         </tr>  
  22. </table>  
  23. }  
  
I want user to select client name and click on submit. This saves the selected client name in a global variable which will be used across pages.
 
Home Controller
 
  1. namespace WebApplication1.Controllers  
  2. {  
  3.     public class HomeController : Controller  
  4.     {  
  5.         MdlCnStringContextClass _mdlCntxtcls = new MdlCnStringContextClass();  
  6.         GlobalVaCls GVaCls = new GlobalVaCls();  
  7.         GlblAttendanceCls glblAttendanceCls = new GlblAttendanceCls();  
  8.         ClientMdl clntMdl = new ClientMdl();  
  9.   
  10.         /*        public ActionResult Index() 
  11.                 { 
  12.                     Client clntMdlObj = new Client(); 
  13.                     clntMdlObj.Clients = GetAllClientNames(); 
  14.                     return View(clntMdlObj); 
  15.                 } 
  16.  
  17.  
  18.  
  19.                 private List<SelectListItem > GetAllClientNames() 
  20.                 { 
  21.                     List<SelectListItem> CNames = new List<SelectListItem>(); 
  22.                     CNames = _mdlCntxtcls.clients.Select( c => new SelectListItem { Value=c.ClientID.ToString(),Text=c.Client_Names.ToString()}).ToList(); 
  23.                     return (CNames); 
  24.                 }*/  
  25.   
  26.         [HttpGet]  
  27.         public ActionResult Index()  
  28.         {  
  29.             clntMdl.Clients = GetClientNames(clntMdl);  
  30.             int InOutRws = glblAttendanceCls.Attendance();  
  31.             if (GlobalVaCls.SelectedClient != null)  
  32.             {  
  33.                 ViewBag.VBg = GlobalVaCls.SelectedClient;  
  34.                 ViewBag.InOtRwsCnt = InOutRws;  
  35.   
  36.                 if (InOutRws > 0)  
  37.                 {  
  38.                     var vClientNames = _mdlCntxtcls.Clients  
  39.                         .Select(c => c.Client_Names);  
  40.                           
  41.   
  42.                     return View(clntMdl);  
  43.                 }  
  44.                 else  
  45.                 {  
  46.                     return View(clntMdl);  
  47.                 }  
  48.             }  
  49.             else  
  50.             {  
  51.                 return View(clntMdl);  
  52.             }  
  53.   
  54.         }  
  55.   
  56.         [HttpPost ]  
  57.         public ActionResult Index(ClientMdl client)  
  58.         {  
  59.             int InOutRws = glblAttendanceCls.Attendance();  
  60.             ViewBag.InOtRwsCnt = InOutRws;  
  61.             //Use this statement when getting the value of Dropdown text from View  
  62.             string Client_Names = client.Client_Names.ToString();  
  63.   
  64.             //Gets the Client name from textbox in Index View  
  65.             //string strClient = client.Client_Names;  
  66.             //ViewBag.M = strClient;  
  67.             GlobalVaCls.SelectedClient = client.Client_Names;  
  68.             clntMdl.Clients = GetClientNames(clntMdl);  
  69.             return View(clntMdl);  
  70.         }  
  71. }  
 
When i am on Attendance page and clicking Home link to browse to Home page. I want the client name which was saved in global variable to be the text of the dropdownlist in Home page.
 
But i am facing an error as Object not set to an Instance in this statement
 
  1. @Html.DropDownListFor(model => model.Client_Names, new SelectList(Model.Clients, "Client_Names""Client_Names"), "Select Client"new { onchange = "document.getElementById('Frm1').submit();" })   
 Here is where i need your help. Please revert if you need more clarifications.
 
 

Answers (3)