我何时使用视图模型、部分、模板并通过 MVC 3 处理子绑定

发布于 2024-11-10 04:17:36 字数 299 浏览 6 评论 0原文

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

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2024-11-17 04:17:36

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

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

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

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

不仅视图会被重用。部分可用于使您的视图更加结构化。另外,如果您使用 AJAX,部分功能会更容易。您可以将 AJAX 请求发送到控制器操作,该操作将返回部分视图,允许您仅更新 DOM 的部分内容。

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

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

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

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

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

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

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

然后是控制器:

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

最后是视图:

@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" />
}

最后一部分是子项的编辑器模板 (~/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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文