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
- @model WebApplication1.Models.ClientMdl
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>ATOM</h2>
- @using (Html.BeginForm("Index", "Home",FormMethod.Post,new { id="Frm1"}))
- {
- <table>
- <tr></tr>
- <tr>
- <div>
- Select Client name<br />
- @Html.DropDownListFor(model => model.Client_Names, new SelectList(Model.Clients, "Client_Names", "Client_Names"), "Select Client", new { onchange = "document.getElementById('Frm1').submit();" })
-
- @* @Html.TextBoxFor(model => model.Client_Names)*@
- @Html.ValidationMessageFor(model => model.Client_Names)
- <input type="submit" value="Submit" />
-
- </div>
- </tr>
- </table>
- }
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
- namespace WebApplication1.Controllers
- {
- public class HomeController : Controller
- {
- MdlCnStringContextClass _mdlCntxtcls = new MdlCnStringContextClass();
- GlobalVaCls GVaCls = new GlobalVaCls();
- GlblAttendanceCls glblAttendanceCls = new GlblAttendanceCls();
- ClientMdl clntMdl = new ClientMdl();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [HttpGet]
- public ActionResult Index()
- {
- clntMdl.Clients = GetClientNames(clntMdl);
- int InOutRws = glblAttendanceCls.Attendance();
- if (GlobalVaCls.SelectedClient != null)
- {
- ViewBag.VBg = GlobalVaCls.SelectedClient;
- ViewBag.InOtRwsCnt = InOutRws;
-
- if (InOutRws > 0)
- {
- var vClientNames = _mdlCntxtcls.Clients
- .Select(c => c.Client_Names);
-
-
- return View(clntMdl);
- }
- else
- {
- return View(clntMdl);
- }
- }
- else
- {
- return View(clntMdl);
- }
-
- }
-
- [HttpPost ]
- public ActionResult Index(ClientMdl client)
- {
- int InOutRws = glblAttendanceCls.Attendance();
- ViewBag.InOtRwsCnt = InOutRws;
-
- string Client_Names = client.Client_Names.ToString();
-
-
-
-
- GlobalVaCls.SelectedClient = client.Client_Names;
- clntMdl.Clients = GetClientNames(clntMdl);
- return View(clntMdl);
- }
- }
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
- @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.