as well as here with given model,controller and view of this page.
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LC_PRO.DAL;
using LC_PRO.Models;
using LC_PRO.Model;
namespace LC_PRO.Controllers
{
public class AddUserController : Controller
{
BAL.BAL objBal = new BAL.BAL();
EmplyInfoModel objEmplyInfo = new EmplyInfoModel();
[HttpGet]
public ActionResult UserAdd()
{
objEmplyInfo.EmplyInformationModel = GridLoadEmplyDetails();
return View(objEmplyInfo);
}
[HttpPost]
public ActionResult UserAdd(BindBase data)
{
return RedirectToAction("UserAdd", "Adduser");
}
public List<EmplyInformation> GridLoadEmplyDetails()
{
List<EmplyInformation> GetEmplyName = new List<EmplyInformation>();
using (var Emp_data = new LC_PRODataContext())
{
GetEmplyName = (from emp in Emp_data.Emplies
select new EmplyInformation
{
Emp_Firstname = emp.Emply_Frst,
Emp_Lastname = emp.Emply_Lst,
emplyId = emp.Emply_Id,
RightsModel = GetRights(emp.Emply_Id)
}).ToList<EmplyInformation>();
}
return GetEmplyName;
}
public SelectList GetRights(int emplyId)
{
List<RightsModel> objRights = new List<RightsModel>();
using (var data = new LC_PRODataContext())
{
objRights = (from em in data.EmplyInfos
join er in data.Emply_Rights on em.Rights_Id equals er.Rights_Id
// where em.Emply_Id.Equals(emplyId)
select new RightsModel
{
RightsId = em.Rights_Id,
RightsName = er.Rights_Name,
}).ToList<RightsModel>();
}
SelectList objlistoEmplyTobind = new SelectList(objRights, "RightsId", "RightsName");
return objlistoEmplyTobind;
}
}
}
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LC_PRO.Model
{
public class EmplyInfoModel
{
public List<EmplyInformation> EmplyInformationModel { get; set; }
}
public class EmplyInformation
{
public string Emp_Firstname { get; set; }
public string Emp_Lastname { get; set; }
public int emplyId { get; set; }
public SelectList RightsModel { get; set; }
}
public class RightsModel
{
public int? RightsId { get; set; }
public string RightsName { get; set; }
public int? selected { get; set; }
}
}
View
@model LC_PRO.Model.EmplyInfoModel
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/MasterLayout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<title>UserAdd</title>
<link href="../../Content/Common.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<table align="center" width="980">
<tr>
<td align="center">
@using (@Html.BeginForm())
{
var grid = new WebGrid(source: Model.EmplyInformationModel, rowsPerPage: 10);
@grid.GetHtml(tableStyle: "gridTable",headerStyle: "gridHead",footerStyle: "gridFooter",rowStyle: "gridRow",alternatingRowStyle: "gridAltRow",
columns: grid.Columns(
grid.Column("Emp_Firstname", "Emp_Firstname"),
grid.Column("Emp_Lastname", "Emp_Lastname"),
grid.Column(header: "Rights", format: @item => Html.DropDownList("value", (IEnumerable<SelectListItem>)Model.EmplyInformationModel[0].RightsModel))))
}
</td>
</tr>
</table>
</div>
</body>
</html>
Kannappan, if this helps please login to Mark As Answer. | Alert Moderator