简体   繁体   English

ASP.NET MVC模型绑定器。 使用对象数组参数调用控制器

[英]ASP.NET MVC Model Binders. Call a controller with an array of objects parameter

I would like to have the following signature of a MVC controller. 我想拥有以下MVC控制器的签名。

public ActionResult Create(Persons[] p)
{

}

Is it possible to have something like this? 可能有这样的事情吗?

EDIT: Let's say I would like to obtain an array of person objects by submitting a list of names separated by a special symbol. 编辑:假设我想通过提交由特殊符号分隔的名称列表来获得人员对象数组。 For example I submit a form 例如我提交一个表格

<form>
   <input name="person_name">Max|Alex|Andrew</input>
</form>

I suppose it should be done by implementing a IModelBinder interface but I didn't find any example how to do this. 我想应该通过实现IModelBinder接口来完成,但是我没有找到任何执行此操作的示例。

Oh I've found how to do this. 哦,我已经找到方法。

In the Global.asax file in the Application_Start() function should be written: 在Application_Start()函数的Global.asax文件中应编写:

ModelBinders.Binders(typeof(Person[])) = new PersonBinder();

In this manner a custom binder is registered. 以这种方式注册了一个定制的活页夹。

Yes. 是。 See this question for how to get collections binded by the DefaultModelBinder . 这个问题如何获得通过绑定的集合DefaultModelBinder

我认为这种事情会起作用

<input type="text" name="Person[1].name" />

you may need to play around with a custom controlleractioninvoker . 您可能需要尝试使用自定义controlleractioninvoker

or just a plain modelbinder 或只是一个普通的模型绑定器

Andrew 安德鲁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM