ASP.NET MVC - 究竟是如何使用视图模型 [英] ASP.NET MVC - How exactly to use View Models

查看:84
本文介绍了ASP.NET MVC - 究竟是如何使用视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个页面,允许的用户的详细信息编辑,所以我有这样的视图模型:

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

这是做了正确的方法是什么?他们是两个视图模型?如果是这样,有一个命名约定,我应该用这样我就可以虚拟机的机型一样和虚拟机,仅仅包含页面数据区分?

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:


  1. 创建独立的视图模型类的页面上的每个表格,然后我渲染这些类与PartialViews为 @ {Html.RenderPartial(PartialName,Model.PartialModel);}

  2. 如果页面包含的东西,如HTML METAS我做分离类METAS并把它在部分网页上。

  3. 休息的情况下,如我应该把这个分开上课吗?是你的判断。

因此​​,例如,你必须有某种登录/注册栏或弹出的页面什么的。

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

关于编辑模板<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html\">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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆