模型绑定器中的模型绑定 [英] Model Binding within a Model Binder

查看:81
本文介绍了模型绑定器中的模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,在这里忍受我.我有一个自定义模型活页夹,它可以成功地将表单数据映射到一个自定义对象.在此模型活页夹中,它还将表单项映射到不同的自定义对象.我觉得我应该能够做的是创建一个单独的模型联编程序来处理第二个映射.这是简化版.

Firstly, bear with me here. I have a custom model binder which is successfully mapping form data to a custom object. Within this model binder it also maps form items to different custom object. What I feel I should be able to do is create a separate model binder to take care of this second mapping. This is a simplified version.

自定义对象:

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
    public string Description { get; set; }
    public IEnumerable<SubCategory> SubCategories { get; set; }
}

public class SubCategory
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Status { get; set; }
}

如果我的表单传回一堆SubSubd的ID,则我需要执行的操作是运行到数据存储库并为SubCategory对象添加水合物.在表格中,子类别列表将以以下格式提交:

If my form passes back a bunch of Ids for the SubCategories, what I need to do is run off to the data repository and hydrate the SubCategory object. From the form, a list of subcategories would be submitted in the following format:

<input type="text" name="Name" value="This Category" />

<input type="hidden" name="subcat.Index" value="0" />
<select name="subcat[0].Id">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

<input type="hidden" name="subcat.Index" value="1" />
<select name="subcat[1].Id">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

<input type="hidden" name="subcat.Index" value="2" />
<select name="subcat[2].Id">
    <option value="1">Something</option>
    <option value="2">Something else</option>
</select>

写一个自定义来映射类别显然很简单,写一个模型绑定器来映射SubCategory(在模型绑定器中,我将在查询我的数据存储库中运行)证明有点困难.

Writing a custom to map the Category is obviously simple, writing the model binder which will in turn map the SubCategory (within the model binder I would run off an query my data repository) is proving a little difficult.

我不确定我对此有多清楚,对不起,感谢您的阅读,请告诉我是否有什么话可以说得更清楚!

I am not sure how clear I have made this, apologies, thanks for reading and please let me know if there is something I can say to make this clearer!

推荐答案

我对此的看法是,模型绑定器应该在构造表示模型,而不是存储库中的实体类型.模型绑定器应该是从表单的键/值集合到表示模型的非常简单的映射,该表示模型主要是标量值,并且可能与其他类型(大多数是标量值或列表)有某种关系.正如您所发现的,必须从存储库中实现实体实例会增加很多复杂性.

My take on this is that model binders should be constructing presentation models, not entity types from your repository. The model binder should be a very simple mapping from the key/value collection of the form to a presentation model which is mostly scalar values with possibly some relationships to other types that are mostly scalar values or lists. Having to materialize entity instances from a repository adds a lot of complication, as you have found.

此外,这是不必要的.使用表示模型具有许多优点,包括:

Moreover, it's unnecessary. Using a presentation model has a large number of advantages, including:

  • 因为展示模型仅包含那些字段,所以永远不需要将允许用户更新的字段列入白名单.
  • 除了最复杂的模型绑定方案以外,默认模型绑定器将适用于所有模型.在实践中,我发现只有在用户看到的值必须以有条件的方式绑定到其他某个值时,才需要使用自定义模型绑定程序.使用表示模型时,表示模型的结构应与页面的结构匹配,因此出于结构原因,您无需使用自定义模型绑定器.
  • 您将能够在创建数据库或实体模型之前创建视图和控制器.这意味着您可以在进行大量工作来创建最终系统之前让客户接受您的设计.这有助于在实体模型发生之前解决结构性问题.只需创建一个与您认为客户想要查看的页面相匹配的演示模型,然后使用该演示模型的虚构实例构建页面的总体轮廓,然后将其展示给客户即可.如果他们满意,则可以构建存储库/实体模型并编写LINQ查询以将其映射到您的演示模型.

因此,在您的示例中,子类别将作为整数列表从表单集合中出现.因此,表示模型应具有相同的整数列表.在控制器中,绑定后,您可以调用一种方法,以将模型值从表示模型转移到存储库中的物化类别实例.

So in your example, the subcategories would come in from the form collection as a list of integers. Therefore, the presentation model should have the same list of integers. In the controller, after binding, you can call a method to transfer the model values from the presentation model to a materialized category instance from the repository.

这篇关于模型绑定器中的模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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