After selecting employee from dropdownlist it doesnt show anything
EMPLOYEE MODEL
- namespace Mactest1.Models
- {
- using System;
- using System.Collections.Generic;
- using System.Web.Mvc;
- public partial class Newemployee
- {
- public Newemployee()
- {
- this.Newemployeetrans = new HashSet
(); - }
- public decimal EmpId { get; set; }
- public string EmpName { get; set; }
- public string Designation { get; set; }
- public string Address { get; set; }
- public string City { get; set; }
- public string TelephoneNumber { get; set; }
- public List
Emplist { get; set; } - public IEnumerable
emplist - {
- get
- {
- return new SelectList(Emplist, "EmpId", "EmpName");
- }
- }
- public virtual ICollection
Newemployeetrans { get; set; } - }
- }
EMPLOYEE CONTROLLER
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Mactest1.Models;
- using System.Data.Objects.SqlClient;
- using Mactest1.Controllers;
- namespace Mactest1.Controllers
- {
- public class EmployeeController : Controller
- {
- VBTEST2Entities db = new VBTEST2Entities();
- public ActionResult Employee()
- {
- Newemployee emps = new Newemployee();
- Class1 c = new Class1();
- emps.Emplist = c.GetEmpName();
- return View(emps);
- }
- [HttpPost]
- public ActionResult Employee1(Newemployee model)
- {
- Newemployee emps = new Newemployee();
- var emp = emps.Emplist.Where(e => e.EmpId == model.EmpId).FirstOrDefault();
- emps.EmpId = emp.EmpId;
- emps.EmpName = emp.EmpName;
- emps.Designation = emp.Designation;
- return View(emps);
- }
- }
- }
CLASS FILE TO RETRIEVE DATA FROM MODEL
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Mactest1.Models;
- using System.Data.Objects.SqlClient;
- namespace Mactest1.Controllers
- {
- public class Class1
- {
- public List
GetEmpName() - {
- VBTEST2Entities db = new VBTEST2Entities();
- var y = from s in db.Newemployees
- select s;
- return (y.ToList());
- }
- }
- }
VIEW
- @model Mactest1.Models.Newemployee
- @using System.Linq
- @{
- Layout = null;
- }
- "stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
- "viewport" content="width=device-width" />
Index - class="container">class="row">class="col-sm-4">
- @using (Html.BeginForm("Employee", "Employee", FormMethod.Post, new { id = "demoForm", name = "demoForm" }))
- {
- @Html.Label("Employee Name")
- @Html.DropDownListFor(m => m.EmpName, Model.emplist, "-SELECT Employee-", new { id = "Ddl" })
class="display-label">- @Html.DisplayNameFor(model => model.EmpId)
class="display-field">- @Html.DisplayFor(model => model.EmpId)
class="display-label">- @Html.DisplayNameFor(model => model.EmpName)
class="display-field">- @Html.DisplayFor(model => model.EmpName)
class="display-label">- @Html.DisplayNameFor(model => model.Designation)
class="display-field">- @Html.DisplayFor(model => model.Designation)
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanwar RanwaPosted Jul 24, 2018, 6:10 AM