如何使用绑定preFIX? [英] How to use Bind Prefix?

查看:315
本文介绍了如何使用绑定preFIX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,如果我在数据库有此表:产品

Say if I had this table in my db: Product

它有

ProductId
ProductName
ProductType

现在不管是什么原因,我不能说出我的文本框,产品名称和ProductType所以现在我查看方法应该是这样的。

Now for whatever reason I can't name my textboxes ProductName and ProductType so now my View Method would look like this

public ViewResult Test([Bind(Exclude ="ProductId")] Product)

所以,现在通过我周围的一切都不打就在这款产品,因为它们有不同的名称相匹配。

So now through my playing around nothing would be matched in this product since they have different names.

所以我想这是在preFIX会来,但我不知道如何使用它。也不是我怎么使用它,并排除在同一时间。

So I guess this is where Prefix would come in but I don't know how to use it. Nor how do I use it and Exclude at the same time.

有人可以给我一个例子吗?

Can someone give me an example?

推荐答案

在preFIX作如下...

The prefix is used as follows if in your view you have...

<select name="p.ProductType">....</select>
<input type="text" name="p.ProductName" />

您可以通过做一些像

public ActionResult([Bind(Prefix="p")]Product product)

您应该注意的是MVC会,如果你命名你的方法参数p自动为您做到这一点。

You should note that MVC would do this automatically for you if you named your method argument p.

如果你想在同一时间绑定多个实体的preFIX是非常有用的(例如,两个名称字段)。

The prefix can be very useful if you're trying to bind multiple entities at the same time (e.g. two name fields).

要使用排除绑定到特定的属性(即避人传递ProductIds在伪造的形式)刚刚成立的属性名称排除

To use the exclude binding to certain Properties (i.e. avoid people passing in ProductIds in a forged form) just set the property names to exclude

 public ActionResult([Bind(Prefix="p", Exclude="ProductId")]Product product)

这将确保你的实体产品编号永远不会被设定。

This will ensure that the ProductId on your entity never gets set.

如果你想绑定例如两个完全不同的字段名称键入ProductType你可以看一下自定义模型绑定或者只是抓住了现场出的FormCollection自己。

If you want to bind two completely different field names e.g. Type to ProductType you can look at custom model binding or just grabbing the field out the FormCollection yourself.

这篇关于如何使用绑定preFIX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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