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);
}
vergel aranasPosted Jan 21, 2013, 6:06 AM
Jignesh TrivediPosted Jan 21, 2013, 4:08 AM
hi,
As per my understanding you required more then one entity class properties to render view correct me?
if yes then you may create custom model that containing all the properity that you required.
example
namespace WepApp.Models
{
public class Detail
{
public string MenuName {get;set;}
public string Name {get;set;}
public string DepartmentName {get;set;}
public string Description {get;set;}
public bool CanView {get;set;}
public bool CanAdd {get;set;}
public bool CanEdit {get;set;}
public bool IsActive {get;set;}
}
}
now create strongly typed view.
in html view MVC 3.0
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
in razor view mvc 3.0
@inherits System.Web.Mvc.ViewPage
in mvc 4.0 ViewPage is replace with WebViewPage.
hope this will help you.