using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcList.Models
{
public class EmployeeViewModel
{
public IEnumerable
public IEnumerable
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcList.Models;
using System.Text;
namespace MvcList.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
EmployeeDBContext db = new EmployeeDBContext();
List<SelectListItem> list = new List<SelectListItem>();
foreach(Emp e in db.Emps)
{
SelectListItem item = new SelectListItem()
{
Text = e.Name,
Value = e.EmpID.ToString(),
Selected=false
};
list.Add(item);
}
EmployeeViewModel viewModel = new EmployeeViewModel();
viewModel.empList = list;
return View(viewModel);
}
[HttpPost]
public string Index(IEnumerable<string> selectedEmp)
{
if(selectedEmp==null)
{
return "You have not selected any employee";
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append("Selected employeeID " + string.Join(" ," , selectedEmp));
return sb.ToString();
}
}
}
}
@model MvcList.Models.EmployeeViewModel
@{
ViewBag.Title = "Employee List";
}
<h2>Employee List</h2>
<div>
@using(Html.BeginForm())
{
@Html.ListBoxFor(x => x.selectedEmp, Model.empList)
<br /><br />
<input type="submit" value="Submit" />
}
</div>