【问题标题】:mvc validating binded to entitiy with reference to other entitiesmvc 验证绑定到实体并引用其他实体
【发布时间】:2011-10-28 12:34:12
【问题描述】:

您好,刚刚考虑在控制器中映射和绑定我的实体。 我应该如何正确绑定模型中的实体,以便我可以使用模型状态

我正在使用 MenuItemModel 创建新的 MenuItem。

public class MenuItemModel
{
    public List<SelectListItem> Menus { get; set; }
    public MenuItem MenuItem { get; set; }
}

我的 MenuItem 类定义如下:

public class MenuItem:Entity
{

    public virtual int MenuItemId { get; set; }
    public virtual Menu Menu { get; set; }
    [Required]
    public virtual string Name { get; set; }
    public virtual int ItemOrder { get; set; }
    public virtual string ExternalUrl { get; set; }
    public virtual DateTime Created { get; set; }
    public virtual bool Deleted { get; set; }
    public virtual DateTime? DisplayUntil { get; set; }
    public virtual User Author { get; set; }

}

现在当我在控制器中绑定我的实体时。

    //
    // POST: /Administrator/MenuItem/Create

    [HttpPost]
    public ActionResult Create(MenuItem menuItem)
    {
        if (ModelState.IsValid)
        {
            // do saving logic
        menuItem.Created = DateTime.Now;
        menuItem.Author = this._userProvider.GetCurrentUser();
        menuItem.Menu = _menuRepository.Load(menuItem.Menu.MenuId);

        }
        //restore
        MenuItemModel menuItemModel = new MenuItemModel();
        menuItemModel.MenuItem = menuItem;
        menuItemModel.Menus =
            this._menuRepository.All.Select(x => new SelectListItem() { Text = x.Name, Value = x.MenuId.ToString() }).ToList();

        return View(menuItemModel);
    }

唯一的问题是我不仅要验证 MenuItem,还要验证 Menu、User。

如何将此验证设置为仅接受 MenuItem 实体的验证?

PS 我知道我可以进入模型状态项并仅找到我需要的实体并检查它们是否有效,但我相信会有更好的方法来做到这一点......

任何想法都值得赞赏。

【问题讨论】:

    标签: asp.net-mvc-2 entity model-binding


    【解决方案1】:

    如何将此验证设置为仅接受 MenuItem 实体的验证?

    您应该使用仅包含需要在控制器操作中验证的属性的视图模型(通常是包含在表单中并由用户输入的属性)。视图模型是专门为给定视图的要求而设计的类。控制器操作不应该将域模型传递到视图/从视图传递/获取域模型。控制器操作应始终将视图模型传递到视图/从视图传递/获取视图模型。例如:

    public class MenuItemViewModel
    {
        public int MenuItemId { get; set; }
    
        [Required]
        public string Name { get; set; }
    
        ... put any properties that are contained on the form with their
            respective validation
    }
    

    然后让您的 POST 控制器操作将此视图模型作为参数:

    [HttpPost]
    public ActionResult Create(MenuItemViewModel viewModel)
    {
        if (!ModelState.IsValid)
        {
            // there were some validation errors => redisplay the view
            // so that the user can fix them
            return View(viewModel);
        }
    
        // at this stage validation went fine
        // TODO: map the view model back to a domain model 
        // (MenutItem or whatever you are willing to update)
        // I would recommend you AutoMapper for this task: http://automapper.codeplex.com/
    
        // TODO: once you get the domain model pass it to a service layer
        // method in order to perform the necessary business operation with it
        // (in your case creating a menu item)
    
        return RedirectToAction("Success");
    }
    

    【讨论】:

    • 感谢 automapper 的提示,已经阅读了一些关于它的信息,但从未使用过。 thnx 4 也回答了....
    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 2014-08-30
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2015-03-12
    • 2023-02-23
    • 1970-01-01
    相关资源
    最近更新 更多