mvc 参考其他实体验证绑定到实体

发布于 2024-11-29 19:51:51 字数 1742 浏览 5 评论 0原文

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

我正在使用 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我知道我可以进入模型状态项目并仅找到我需要的实体并检查它们是否有效,但我相信会有更好的方法来做到这一点......

任何想法都值得赞赏。

Hi just thought about mapping and binding my entity in controller.
How should i correctly bind entity in model so i can use modelstate

I am creating new MenuItem using MenuItemModel.

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

where my MenuItem class is defined as follows:

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; }

}

now when i bind my entity in controller.

    //
    // 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);
    }

the only problem is i am getting validation not for only MenuItem but for Menu, User too.

How shall set this validation to accept validation only for MenuItem Entity ?

PS i know that i can go into modelstate items and find only the entities that i need and check if they are valid but i believe there will be better way of doing this...

Any idea is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2024-12-06 19:51:51

如何设置此验证以仅接受 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");
}

How shall set this validation to accept validation only for MenuItem Entity ?

You should use a view model which contains only the properties that are needed to be validated in your controller action (usually those are the properties contained on the form and entered by the user). View models are classes which are specifically designed for the requirements of a given view. A controller action should never pass/take a domain model to/from a view. A controller action should always pass/take a view model to/from a view. For example:

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
}

then have your POST controller action take this view model as argument:

[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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文