如何禁用默认模式为特定的类绑定 [英] How to disable default model binding for a particular class

查看:93
本文介绍了如何禁用默认模式为特定的类绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用特定类模型绑定,更具体的我不希望被默认的模型绑定设置一类特定的数据属性。

I want to disable model binding for a particular class , to be more specific i don't want certain data property of a class to be set by the default model binding .

如。我有高度和类,我想由用户明确设置的宽度财产

eg . i have height and width property of a class , which i want to be explicitly set by the user

像model.height = XXX;

like model.height = xxx ;

的价值不应该被默认模型活页夹进行设置,即使我通过模型的操作方法。

the value should not be set by default model binders even when i pass the model to the action method.

是有办法做到这一点。

推荐答案

看看的的 Exlude ,的包含的的性能=http://msdn.microsoft.com/en-us/library /system.web.mvc.bindattribute.aspx相对=nofollow> BindAttribute

Take a look at the Exlude, Include properties of BindAttribute.

public ActionResult Create([Bind(Exclude="Height")]MyModel model)

恕我直言,这是更好地使用包括比这样你是在什么样的属性应该被自动绑定明确的,因为排除。使用排除可能是一个安全风险,并可能被黑客利用来设置不旨在通过粘合剂设置的属性

IMHO it's better to use Include than Exclude because in this way you are explicit at what properties should be automatically bound. Using Exclude might be a security risk and could be exploited by a hacker to set properties that are not intended to be set by the binder.

这篇关于如何禁用默认模式为特定的类绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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