我什么时候使用视图模型、部分、模板并使用 MVC 3 处理子绑定 [英] When do I use View Models, Partials, Templates and handle child bindings with MVC 3

查看:17
本文介绍了我什么时候使用视图模型、部分、模板并使用 MVC 3 处理子绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mvc3 新手,我有几个问题,如果有人能回答/提供链接,我将不胜感激:

  1. 我应该什么时候使用视图模型?不建议使用域吗?我发现我的视图模型是我的域对象的副本,并且看不到值...
  2. 我应该什么时候使用 Partials?是不是只有局部视图才会被重用?
  3. 我应该什么时候使用显示模板和编辑器模板?我可以在没有视图模型的情况下使用这些吗?
  4. 如何创建父对象和子对象列表均可编辑的编辑屏幕?即顶部(父)的几个字段和下面的字段网格(如可编辑行),特别是,我如何进行绑定?不使用自动映射器.

谢谢!

解决方案

我应该什么时候使用视图模型?不建议使用域吗?我发现我的视图模型是我的域对象的副本,并且没有看到值...

应始终使用视图模型.您不应在视图中使用域模型.

视图模型不是领域模型的精确副本.他们总是有一些与视图的具体要求相关的差异.例如,您希望在一个屏幕上显示域模型的某些属性,而在其他屏幕上显示其他属性.因此,您还将有不同的验证要求,因为在一个屏幕上需要一个属性,而在其他屏幕上不需要.因此,您还将在这些视图模型上拥有不同的数据注释.

<块引用>

我应该什么时候使用 Partials?是不是只有局部视图才会被重用?

不仅要重用视图.部分可用于使您的视图更有条理.此外,如果您使用 AJAX,partials 会更容易.您可以将 AJAX 请求发送到控制器操作,该操作将返回一个局部视图,允许您仅更新 DOM 的一部分.

<块引用>

我应该什么时候使用显示模板和编辑器模板?我可以在没有视图模型的情况下使用这些吗?

总是.您可以将它们与任何强类型模型一起使用,但您应该始终使用视图模型(请参阅上一个问题的答案).

<块引用>

如何创建父对象和子对象列表均可编辑的编辑屏幕?即顶部(父)的几个字段和下面的字段网格(如可编辑行),特别是,我如何进行绑定?未使用自动映射器.

这是一个相当广泛的问题,但要像往常一样回答它,您首先要定义您的视图模型,该模型将表示/包含您希望在此屏幕上显示以进行编辑的属性:

公共类 ChildViewModel{[必需的]公共字符串名称 { 获取;放;}}公共类 ParentViewModel{[必需的]公共字符串名称 { 获取;放;}公共 IEnumerable孩子{得到;放;}}

然后是控制器:

公共类 HomeController:控制器{公共 ActionResult 索引(){//TODO:从您的存储库中获取实际的域模型,//并将其映射到视图模型(AutoMapper 是一个很好的工具)var 模型 = 新的 ParentViewModel{Name = "父母姓名",Children = Enumerable.Range(1, 5).Select(x => new ChildViewModel{姓名 = "孩子" + x})};返回视图(模型);}[HttpPost]公共 ActionResult 索引(ParentViewModel 模型){如果 (!ModelState.IsValid){返回视图(模型);}//TODO:此处的模型对象将包含新值//=>用户 AutoMapper 将其映射回域模型//并将这个域模型传递给存储库进行处理return RedirectToAction("索引");}}

最后是视图:

@model ParentViewModel@using (Html.BeginForm()){<h2>父</h2><div>@Html.LabelFor(x => x.Name)@Html.EditorFor(x => x.Name)@Html.ValidationMessageFor(x => x.Name)

<h2>儿童</h2><表格><头><tr><th>孩子的名字</th></tr></thead>@Html.EditorFor(x => x.Children)</tbody><input type="submit" value="OK"/>}

最后一块是孩子的编辑器模板(~/Views/Home/EditorTemplates/ChildViewModel.cshtml):

@model ChildViewModel<tr><td>@Html.LabelFor(x => x.Name)@Html.EditorFor(x => x.Name)@Html.ValidationMessageFor(x => x.Name)</td></tr>

new to mvc3, i have a few questions, would appreciate if someone could answer/provide links:

  1. When should I use View Models? Is it not recommended to use the domain? I find that my view models are copies of my domain objects, and don't see the value...
  2. When should I use Partials? Is it only if the partial view will be reused?
  3. When should I use Display Templates and Editor Templates? Can I use these without a view model?
  4. How do I create an edit screen where the parent and list of child objects are both editable? i.e. a few fields at the top (parent) and a grid of fields below (like editable rows), particularly, how do i do the binding? automapper is not used.

Thanks!

解决方案

When should I use View Models? Is it not recommended to use the domain? I find that my view models are copies of my domain objects, and don't see the value...

View models should always be used. You should not use your domain models in the view.

View models are not exact copies of the domain models. They always have some differences related to the specific requirements of the view. For example on one screen you would like to present some of the properties of your domain model and on other screen other properties. As a consequence to this you will also have different validation requirements as one property will be required on one screen and not required on other screen. So you will also have different data annotations on those view models.

When should I use Partials? Is it only if the partial view will be reused?

Not only if the view will be reused. Partials could be used to make your views more structured. Also if you are using AJAX, partials make it easier. You would send the AJAX request to a controller action which will return a partial view allowing you to update only portions of the DOM.

When should I use Display Templates and Editor Templates? Can I use these without a view model?

Always. You can use them with any strongly typed model, but you should always use a view models (see answer to previous question).

How do I create an edit screen where the parent and list of child objects are both editable? i.e. a few fields at the top (parent) and a grid of fields below (like editable rows), particularly, how do i do the binding? automapper is not used.

That's a pretty broad question but to answer it as always you start with defining your view models which will represent/contain the properties you would like to present on this screen for editing:

public class ChildViewModel
{
    [Required]
    public string Name { get; set; }
}

public class ParentViewModel
{
    [Required]
    public string Name { get; set; }

    public IEnumerable<ChildViewModel> Children { get; set; }
}

then a controller:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        // TODO: Fetch an actual domain model from your repository,
        // and map it to the view model (AutoMapper is a great tool for the job)

        var model = new ParentViewModel
        {
            Name = "parent name",
            Children = Enumerable.Range(1, 5).Select(x => new ChildViewModel
            {
                Name = "child " + x
            })
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(ParentViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // TODO: the model object here will contain the new values
        // => user AutoMapper to map it back to a domain model
        // and pass this domain model to the repository for processing

        return RedirectToAction("Index");
    }
}

and finally the view:

@model ParentViewModel
@using (Html.BeginForm())
{
    <h2>Parent</h2>
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
        @Html.ValidationMessageFor(x => x.Name)
    </div>

    <h2>Children</h2>
    <table>
        <thead>
            <tr>
                <th>Child name</th>
            </tr>
        </thead>
        <tbody>
            @Html.EditorFor(x => x.Children)
        </tbody>
    </table>
    <input type="submit" value="OK" />
}

and the last piece is the editor template for a child (~/Views/Home/EditorTemplates/ChildViewModel.cshtml):

@model ChildViewModel
<tr>
    <td>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
        @Html.ValidationMessageFor(x => x.Name)
    </td>
</tr>

这篇关于我什么时候使用视图模型、部分、模板并使用 MVC 3 处理子绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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