2015-02-11 54 views
1

在查看页面我想添加两个模型。一个用于角色创建,另一个用于权限视图。 它还将存储在角色权限表(RoleID,PermissionID)中。如何为单个视图添加两个或更多模型?

我试过像下面这样。

模型:角色创建

public class Rolecreate 
{ 
    [Required(ErrorMessage = "Please enter a Rolename", AllowEmptyStrings = false)]   
    public string Rolename { get; set; } 
    [Required(ErrorMessage = "Please enter the Role description", AllowEmptyStrings = false)] 
    public string Roledescription { get; set; } 

} 

控制器:角色创建

// GET: /Role/Create 

    public ActionResult Create() 
    { 
     return View(); 
    } 

    // 
    // POST: /Role/Create 

    [HttpPost] 
    public ActionResult Create(Role role) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Roles.Add(role); 
      db.SaveChanges(); 
      return RedirectToAction("Roles"); 
     } 

     return View(role); 
    } 

型号:权限

public partial class Permission 
{ 
    public int PermissionID { get; set; } 
    public string Permissionname { get; set; } 
    public string Permissiondescription { get; set; } 
} 

但失败。

请帮我解决这个问题。 谢谢

+2

创建包含2个属性,一个用于作用和一个用于权限的图模型。 – 2015-02-11 12:13:38

回答

0

为什么不使用包含对所需对象的引用的模型对象?

public class yourNewViewModel 
{ 
    public RoleCreate model1 {get; set; } 
    public Permissions model2 {get; set;} 
} 
相关问题