ASP.NET MVC绑定属性的方法而不是参数 [英] ASP.NET MVC Bind Attribute on Method instead of Parameter

查看:187
本文介绍了ASP.NET MVC绑定属性的方法而不是参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,你可以这样做:

I'm aware that you can do this:

public ActionResult DoSomething([Bind(Exclude = "CreationDate")] Item item)
{ /*...*/ }

不过,我发现,你还可以将绑定与属性的方法,例如之上。

However, I found that you can also attach that Bind attribute on top of the method, e.g.

[Bind(Exclude = "CreationDate")] 
public ActionResult DoSomething(Item item)
{ /*...*/ }

这是否有相同的效果吗?如果你绑定到多个参数?

Does this have the exact same effect? What if you bind to more than one parameter?

推荐答案

第二种形式是我见过更常见的之一。据我所知,这两种形式做同样的事情。

The second form is the one that I have seen more commonly. To my knowledge, both forms do exactly the same thing.

排除选项采用一个逗号分隔的属性列表。排除列表根本不包括从结合的一个或多个参数;其对剩余的参数没有影响

The Exclude option takes a comma-separated list of attributes. The Exclude list simply excludes one or more parameters from binding; it has no effect on the remaining parameters.

例如:

[Bind(Exclude="ID, Name")]

这篇关于ASP.NET MVC绑定属性的方法而不是参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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