mvc 参考其他实体验证绑定到实体
您好,只是考虑在控制器中映射和绑定我的实体。 我应该如何正确绑定模型中的实体,以便我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用仅包含需要在控制器操作中验证的属性的视图模型(通常这些属性是表单中包含并由用户输入的属性)。视图模型是专门为满足给定视图的要求而设计的类。控制器操作永远不应该将域模型传递给视图或从视图中获取域模型。控制器操作应该始终将视图模型传递到视图或从视图获取视图模型。例如:
然后让您的 POST 控制器操作将此视图模型作为参数:
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:
then have your POST controller action take this view model as argument: