我何时使用视图模型,部分,模板和处理与MVC 3的子绑定

new*_*_86 11 partials modelbinder viewmodel editortemplates asp.net-mvc-3

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

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

谢谢!

Dar*_*rov 23

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

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

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

我什么时候应该使用Partials?只有部分视图可以重用吗?

不仅视图将被重用.部分可用于使您的视图更加结构化.此外,如果您使用的是AJAX,则部分功能会让您更轻松.您可以将AJAX请求发送到控制器操作,该操作将返回部分视图,允许您仅更新DOM的部分.

我应该何时使用显示模板和编辑器模板?没有视图模型我可以使用它们吗?

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

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

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

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

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

    public IEnumerable<ChildViewModel> Children { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后一个控制器:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

最后是观点:

@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" />
}
Run Code Online (Sandbox Code Playgroud)

最后一块是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>
Run Code Online (Sandbox Code Playgroud)