ASP.NET MVC - 如何准确地使用视图模型 [英] ASP.NET MVC - How exactly to use View Models
问题描述
假设我有一个允许编辑用户详细信息的页面,所以我有一个这样的 ViewModel:
Let's say I have a page that allows the editing of a user's details, so I have a ViewModel like this:
public class UserViewModel {
public string Username { get; set; }
public string Password { get; set; }
public int ManagerId { get; set; }
public string Category { get; set; }
}
所以在我的 EditUser 操作中,我可以让模型绑定器将其传回,然后我可以将其映射到域模型:
So on my EditUser action I can have this passed back by the model binder and then I can map that to the Domain Model:
public ActionResult EditUser(UserViewModel user) {
...
但是,显示表单的页面还需要详细信息,例如经理和类别列表,以提供这些字段的下拉列表.它还可能在侧边栏中显示其他用户的列表,以便您可以在正在编辑的不同用户之间切换.
However, the page that displays the form also needs details such as a list of Managers and Categories to provide dropdowns for those fields. It might also display a list of other users in a sidebar so you can switch between the different users you're editing.
那么我有另一个视图模型:
So then I have another view model:
public class ViewUserViewModel {
public UserViewModel EditingUser { get; set; }
public IEnumerable<SelectListItem> Managers { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
public IEnumerable<SelectListItem> AllUsers { get; set; }
}
这是正确的做法吗?他们都是查看模型吗?如果是这样,是否有我应该使用的命名约定,以便我可以区分类似于模型的 VM 和仅包含页面数据的 VM?
Is this the correct way to do it? Are they both View Models? If so, is there a naming convention I should use so I can distinguish between VMs that are like models and VMs that just contain data for the page?
我都弄错了吗?
推荐答案
我如何在快捷方式中做到这一点:
How I do this in shortcut:
- 为页面上的每个表单创建单独的 ViewModel 类,然后我将这些类与 PartialViews 渲染为
@{Html.RenderPartial("PartialName", Model.PartialModel);}
. - 如果页面包含诸如 html metas 之类的内容,我会为 metas 制作单独的类并将其放在页面上的部分中.
- 其他情况,例如我应该将其放在单独的班级中吗?"是你的判断.
例如,您的页面具有某种登录/注册栏或弹出窗口.
So for example you have page which has some kind of login/register bar or popup whatever.
public class SomePageViewModel
{
public RegisterBarVM Register { get; set; }
public LoginBarVM LoginBar { get; set; }
public MetasVM Metas { get; set; }
public string MaybePageTitle { get; set;}
public string MaybePageContent { get; set;}
[HiddenInput(DisplayValue = false)]
public int IdIfNeeded { get; set; }
public IEnumerable<SelectListItem> SomeItems {get; set;}
public string PickedItemId { get;set; }
}
public class RegisterBarVM
{
public string RegisterUsername {get;set;}
public string RegisterPassword {get;set;}
//...
}
public class LoginBarVM
{
public string LoginUserame {get;set;}
public string LoginPassword {get;set;}
//...
}
//cshtml
@model yourClassesNamespace.SomePageViewModel
@{
Html.RenderPartial("LoginBar", Model.LoginBar); //form inside
Html.RenderPartial("RegisterBar", Model.RegisterBar); //form inside
using(Html.BeginForm())
{
@Html.EditorFor(m => m.IdIfNeeded)
@Hmtl.EditorFor(m => m.MaybePageTitle)
@Hmtl.EditorFor(m => m.MaybePageContent)
@Hmtl.DropDownListFor(m => m.PickedItemId, new SelectList(Model.SomeItems))
<input type="submit" value="Update" />
}
}
@section Metas {
@{Html.RenderPartial("Meatas", Model.Metas}
}
关于编辑器模板 Brad威尔逊博客,只需谷歌或寻找有关显示/编辑器模板和 HtmlHelpers 的堆栈资源.它们对于构建一致的网站非常有用.
About editor templates Brad Wilsons Blog and just google or look for stacks resources about display/editor templates and HtmlHelpers. They all are very useful for building consistent websites.
这篇关于ASP.NET MVC - 如何准确地使用视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!