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

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

问题描述

新MVC3,我有几个问题,将AP preciate如果有人能回答/提供链接:


  1. 当我应该使用视图模型?难道不推荐使用的域名?我发现我的视图模型都是我的域对象的副本,并没有看到价值...

  2. 当我应该使用局部模板?难道只有在局部视图将被重用?

  3. 什么时候应该使用显示模板和模板编辑器?我可以用这些没有一个视图模型?

  4. 如何创建一个编辑屏幕,子对象的父和列表都可以编辑?即顶部(父)的几个字段及以下(如编辑的行),特别是,我该怎么做绑定字段的网格? automapper不被使用。

谢谢!


解决方案

  

我什么时候应该使用视图模型?难道不推荐使用的域名?我发现我的视图模型都是我的域对象的副本,并没有看到价值...


查看模型应始终使用。你不应该用你的域模型视图。

查看车型不在域模型的精确副本。他们总是有相关视图的具体要求有所不同。例如在一个屏幕上您想present一些领域模型和其他屏幕的其他属性的属性。其结果是这个你也将作为一个属性会在一个屏幕上需要和不需要其他屏幕上不同的验证要求。所以,你还会对那些视图模型不同的数据注解。


  

当我应该使用局部模板?难道只有在局部视图将被重用?


不仅如果视图将被重用。谐音可以用来使你的观点更有条理。此外,如果你正在使用AJAX,谐音更容易。你会发送AJAX请求到一个控制器动作将返回让你更新DOM只有部分的局部视图。


  

我什么时候应该使用显示模板和模板编辑器?我可以用这些没有一个视图模型?


始终。你可以使用任何的强类型模型使用它们,但你应该总是使用视图模型(见答案previous问题)。


  

如何创建一个编辑屏幕,子对象的父和列表都可以编辑?即顶部(父)的几个字段及以下(如编辑的行),特别是,我该怎么做绑定字段的网格? automapper不被使用。


这是一个pretty广泛的问题,但与定义你的视图模型,这将重新present一如既往你开始回答/包含的属性,你想$此画面进行编辑页上$ psent:

 公共类ChildViewModel
{
    [需要]
    公共字符串名称{;组; }
}公共类ParentViewModel
{
    [需要]
    公共字符串名称{;组; }    公共IEnumerable的< ChildViewModel>儿童{搞定;组; }
}

然后控制器:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        // TODO:从您的存储库中取出一个实际的域模型,
        //并将其映射到视图模型(AutoMapper是工作一个伟大的工具)        VAR模型=新ParentViewModel
        {
            NAME =父名,
            儿童= Enumerable.Range(1,5)。选择(X =>新建ChildViewModel
            {
                NAME =子+ X
            })
        };
        返回查看(模型);
    }    [HttpPost]
    公众的ActionResult指数(ParentViewModel模型)
    {
        如果(!ModelState.IsValid)
        {
            返回查看(模型);
        }        // TODO:这里的模型对象将包含新的值
        // =>用户AutoMapper将其映射回领域模型
        //并通过这一领域模型库进行处理        返回RedirectToAction(「指数」);
    }
}

和最后的观点:

  @model ParentViewModel
@using(Html.BeginForm())
{
    < H2>家长和LT; / H>
    < D​​IV>
        @ Html.LabelFor(X => x.Name)
        @ Html.EditorFor(X => x.Name)
        @ Html.ValidationMessageFor(X => x.Name)
    < / DIV>    < H2>的子< / H2>
    <表>
        <&THEAD GT;
            &所述; TR>
                百分位>儿童的名字和LT; /第i
            < / TR>
        < / THEAD>
        <&TBODY GT;
            @ Html.EditorFor(X => x.Children)
        < / TBODY>
    < /表>
    <输入类型=提交VALUE =OK/>
}

和最后一块是一个孩子的编辑模板(〜/查看/主页/ 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天全站免登陆