ASP.Net MVC - 如何包含/排除某些子集合/枚举的属性的结合? [英] ASP.Net MVC - How to include / exclude binding of certain child collection / enumerable properties?

查看:82
本文介绍了ASP.Net MVC - 如何包含/排除某些子集合/枚举的属性的结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

详细内容:


  • ASP.Net MVC 5

  • .NET 4.5

  • MVVM

  • 实体框架6 code首先

我与他们复杂属性的ViewModels。我看了一下我该怎么使用的 BindAttribute 以包含或排除模型bidning性能,得到了这个工作的基本属性。

I have ViewModels with complex properties on them. I have read about how I can use the BindAttribute to include or exclude properties for model bidning and got basic properties working with this.

不过有一件事我找不到是如何控制集合属性中的子属性的结合。例如,我有以下

However one thing I cannot find is how to control the binding of child properties within a collection property. For example I have the following

[Bind(Include = "Model.Id, Model.Positions.StartDate")]
public class ProjectViewModel
{
    public Project Model {get;set;}

    public ProjectViewModel(Project project)
        : base(project)
    {
          Model = project;
    }

    public ProjectViewModel()
    {

    }

}

一个项目具有位置的列表:

A Project has a list of Positions:

public class Project : BaseEntity
{
    .
    .
    .

    public virtual IList<Position> Positions
    {
        get;
        set;
    }

}

和位置有开始和结束日期:

And a Position has a start and end date:

public class Position : BaseEntity
{
    [SensibleDateTime]
    public DateTime StartDate { get; set; }

    [SensibleDateTime]
    public DateTime EndDate { get; set; }

    [Required]
    [ForeignKey("Project")]
    public Int64 ProjectID { get; set; }
    public virtual Project Project { get; set; }
}

我必须使用项目视图模型,其中允许用户改变这些位置的开始和结束日期位置网格的屏幕。我不想让他们让他们修正位置包括由请求/后操纵的服务器的任何其它性质。

I have a screen using the Project View Model with a grid of positions where the user is allowed to change the start and end date of those positions. I do not want to allow them to let them amend any other properties of the Position including by manipulating the request / post to the server.

我写这从中获取原始的实体从数据库中DefaultModelBinder继承我自己的自定义模型粘合剂。我只是想绑定在上面允许绑定字段来获得这些新的价值观,所有的原始值将已经存在了。

I am writing my own custom model binder which inherits from the DefaultModelBinder which gets the original entity out the database. I just want to bind permitted bound fields on top to get these new values and all the original values will already exist.

我知道我能得到原单位出来的,而不是控制器内的数据库和手动映射界开始和结束日期到实体。我想如果可能的话,以避免这一点,使控制器code尽可能简单,因为这将是一个非常常见的任务。此外,它看起来像使用标准MVC机制将是preferred方法。

I am aware I could get the original entity out the database inside the controller instead and map the bound start and end date onto the entity manually. I want to avoid this if possible and make the controller code as simple as possible as this will be a very common task. Additionally it seems like using standard MVC mechanisms would be the preferred approach.

我曾尝试在项目视图模型下面绑定的语句:

I have tried the following bind statement on the Project View Model:

[Bind(Include = "Model.Positions.StartDate")]

新值没有被绑定,包括位置的开始日期。如果我写一个简单的包括直接在项目视图模或者模属性它的工作原理。

None of the new values are bound including the start date of the positions. If I write a simple include for a property directly on the Project View Model or Model it works.

你怎么写是指属性的实体集合中的绑定属性声明?

How do you write a Bind attribute statement that refers to a property within an entity collection?

推荐答案

我不能肯定地说,我从来没有使用绑定属性,但点语法的工作。不过,我有理由相信,绑定专注于发布名称,而不是实际的模型属性。换句话说,你很可能需要做一些像位置[0] .StartDate,位置[1] .StartDate,...

I can't say for sure as I never use the Bind attribute, but the dot syntax should work. However, I'm reasonably sure that Bind focuses on the posted name, not the actual model property. In other words, you would most likely need to do something like "Positions[0].StartDate,Positions[1].StartDate,...".

在一般情况下,它是更好只使用视图模型只包含的属性你想要编辑的。

In general, it's far better to just use view models to only include the properties you want to be editable.

这篇关于ASP.Net MVC - 如何包含/排除某些子集合/枚举的属性的结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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