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";
- }
-
ATOM
- @using (Html.BeginForm("Index", "Home",FormMethod.Post,new { id="Frm1"}))
- {
-
- Select Client name
- @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)
- "submit" value="Submit" />
- }
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();
- /* public ActionResult Index()
- {
- Client clntMdlObj = new Client();
- clntMdlObj.Clients = GetAllClientNames();
- return View(clntMdlObj);
- }
- private List
GetAllClientNames() - {
- List
CNames = new List (); - CNames = _mdlCntxtcls.clients.Select( c => new SelectListItem { Value=c.ClientID.ToString(),Text=c.Client_Names.ToString()}).ToList();
- return (CNames);
- }*/
- [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;
- //Use this statement when getting the value of Dropdown text from View
- string Client_Names = client.Client_Names.ToString();
- //Gets the Client name from textbox in Index View
- //string strClient = client.Client_Names;
- //ViewBag.M = strClient;
- 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();" })
Suraj KumarPosted Nov 5, 2018, 8:04 AM
jimmit ravalPosted Nov 5, 2018, 7:53 AM
Suraj KumarPosted Nov 5, 2018, 7:48 AM