【问题标题】:How can I add two or more model for a single view?如何为单个视图添加两个或多个模型?
【发布时间】:2015-02-11 20:26:08
【问题描述】:

在视图页面中,我想添加两个模型。一个用于角色创建,另一个用于权限视图。 它还将存储在 Role-Permission 表中 (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 个属性的视图模型,一个用于角色,一个用于权限。

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

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

喜欢

public class yourNewViewModel
{
    public RoleCreate model1 {get; set; }
    public Permissions model2 {get; set;}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2020-02-19
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多