[Bind(Exclude = "AlbumId")] 注释在数据验证中的作用是什么?脚手架是什么意思? [英] What is the role of [Bind(Exclude = "AlbumId")] annotation in data validation? What does scaffolding mean?

查看:24
本文介绍了[Bind(Exclude = "AlbumId")] 注释在数据验证中的作用是什么?脚手架是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循本教程:http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-6

在谈论表单验证时,作者说绑定注释用于:

When talking about form validation author says that Bind anntoations is used to:

 Lists fields to exclude or include when binding parameter or form values to model properties

这对我来说有点胡言乱语 - 我不明白.它实际上是什么意思?也许问题在于脚手架这个词在字典中的含义与 IT 没有任何关系.

It is little bit gibberish to me - I don't get it. What does it actually mean? Maybe the problem is the word scaffold which has to meanings in dictionary that don't have any connection to IT.

结果是什么:[Bind(Exclude = "AlbumId")] 以及打字的意义是什么:[ScaffoldColumn(false)] - 列默认不隐藏,何必再说一遍.

What is the result of: [Bind(Exclude = "AlbumId")] and what is the sense of typing: [ScaffoldColumn(false)] - the column is not hidden by default, why to say it again.

namespace MvcMusicStore.Models
{
    [Bind(Exclude = "AlbumId")]
    public class Album
    {
        [ScaffoldColumn(false)]
        public int      AlbumId    { get; set; }
        [DisplayName("Genre")]
        public int      GenreId    { get; set; }
        [DisplayName("Artist")]
        public int      ArtistId   { get; set; }
        [Required(ErrorMessage = "An Album Title is required")]
        [StringLength(160)]
        public string   Title      { get; set; }
        [Required(ErrorMessage = "Price is required")]
        [Range(0.01, 100.00,
            ErrorMessage = "Price must be between 0.01 and 100.00")]
        public decimal Price       { get; set; }
        [DisplayName("Album Art URL")]
        [StringLength(1024)]
        public string AlbumArtUrl { get; set; }
        public virtual Genre  Genre    { get; set; }
        public virtual Artist Artist   { get; set; }
    }
}

推荐答案

Scaffold

值得一提的是,我在 ASP.NET MVC 之上构建应用程序已经两年多了,而且我从未使用过 ScaffoldColumn 属性.但是,要实际回答您的问题,该属性用于告诉框架该字段是否应该可见.

Scaffold

For what it's worth I've been building apps on top of ASP.NET MVC for over two years now and not once have I ever used the ScaffoldColumn attribute. However, to actually answer your question the attribute is used to tell the framework if the field should be visible.

举个例子,如果你使用了 @Html.EditorForModel()(这个方法在幕后做了一些魔法并为你的模型属性构建表单输入)那么你的视图将 包含在 ScaffoldColumn 属性中标记为 false 的列的输入.它本质上是一种快捷方式,告诉框架在构建视图时不允许编辑该字段.同样,我从不使用 EditorForModel,因为我为每个输入手动构建表单,因为我想要更精细的控制.

As an example, if you used @Html.EditorForModel() (this method does some magic under the hood and builds out form inputs for your model properties) then your view will not contain inputs for the columns marked false in the ScaffoldColumn attribute. It's essentially a shortcut to tell the framework not to allow the field to be edited when it builds out the view. Again, I never use EditorForModel because I build out my forms by hand for each input because I want more granular control.

Bind 属性用于告诉框架在向服务器发送数据时是否允许绑定该属性(这更多的是一种安全功能).

The Bind attribute is used to tell the framework if you want to allow the property to be bound to when sending data to the server (this is more of a security feature).

考虑这个动作:

[HttpPost]
public ActionResult DontSendMeTooMuchData(Employee employee) {
    db.Employees.Update(employee);
}

假设 Employee 对象如下所示:

Suppose the Employee object looks like this:

public class Employee {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SocialSecurityNumber { get; set; }
}

现在假设我发送一个发送数据的 HTTP POST 请求:

Now suppose I send an HTTP POST request that sends up data:

FirstName=Justin
&LastName=Helgerson
&SocialSecurityNumber=YouShouldntLetMeUpdateThis

您刚刚更新了我的社会安全号码!坏消息熊熊.你如何阻止它?

You've just updated my social security number! Bad news bears. How do you stop it?

[Bind(Exclude = "SocialSecurityNumber")]
public class Employee {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SocialSecurityNumber { get; set; }
}

现在,当 DontSendMeTooMuchData 方法被执行并且 ASP.NET MVC 执行模型绑定以从 HTTP 请求中为您提供一个 Employee 对象时,SocialSecurityNumber 属性永远不会从请求数据中填充.

Now when the DontSendMeTooMuchData method is executed and ASP.NET MVC does the model binding to give you an Employee object from the HTTP request, the SocialSecurityNumber property will never be populated from the request data.

同样,这是我从未在我的应用程序中使用的属性.解决这个问题的另一种方法还有其他好处.由于担心这个答案变得太长,解决问题的方法是为您的视图和数据库使用不同模型.我从不让我的数据库对象进入我的视野.相反,我构建了一个专门用于视图的类,该类只包含我希望用户提供的属性.

Again, this is an attribute that I never use in my applications. There is another way to solve this problem that has other benefits. For fear of this answer becoming too long, the way to solve the problem is to use different models for your view and your database. I never let my database objects hit my view. Instead I build a class specifically for use in the view which only contains the properties I want the user to provide.

这篇关于[Bind(Exclude = "AlbumId")] 注释在数据验证中的作用是什么?脚手架是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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