Hi, I really need help for this, I am new to mvc asp.net. My problem is how display the records that i query from different table into the editor view.
public ActionResult Edit(int id = 0)
{
SecurityGroup securitygroup = db.SecurityGroups.Single(s => s.Id == id);
if (securitygroup == null)
{
return HttpNotFound();
}
//collection of records I want to display.
var sql = from a in db.SecurityDetails
join b in db.SecurityGroups on a.SecurityGroupId equals b.Id
join m in db.Menus on a.MenuId equals m.Id
join d in db.Departments on b.DepartmentId equals d.Id where a.SecurityGroupId == id
select new
{
menu = m.MenuName,
name = b.Name,
dept = d.DepartmentName,
desc = b.Description,
view = a.CanView,
add = a.CanAdd,
edit = a.CanEdit,
active = a.IsActive
};
//How can I display these records into the Edit view? Please help me! Thanks
ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DepartmentName", securitygroup.DepartmentId);
return View(securitygroup);
}
//
// POST: /SecurityGroup/Edit/5
[HttpPost]
public ActionResult Edit(SecurityGroup securitygroup)
{
if (ModelState.IsValid)
{
db.SecurityGroups.Attach(securitygroup);
db.ObjectStateManager.ChangeObjectState(securitygroup, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DepartmentName", securitygroup.DepartmentId);
return View(securitygroup);
}