How to use Bind Prefix?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 17:26:14

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

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

You can bind the incoming form to an instance of your model by doing something like

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

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

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!