开发者

How can I deserialize and model bind XML that contains multiple objects to a collection of such objects?

开发者 https://www.devze.com 2023-03-21 14:33 出处:网络
Currently I have a custom Model Binder and Model Binder Provider that detects \"text/xml\", deserializes it using MvcContrib (here is the code/setup), and binds it to a custo开发者_如何学运维m Model t

Currently I have a custom Model Binder and Model Binder Provider that detects "text/xml", deserializes it using MvcContrib (here is the code/setup), and binds it to a custo开发者_如何学运维m Model that I have, for example:

<User>
<name></name><role></role>
</User>

will bind to a new User() that has User.name, user.role (just as you would expect), and the Action of course starts like this:

ActionResult CreateUser(User u) {

Now I am wondering if I can deserialize XML that looks like this:

<Users> 
<User><name></name><role></role></User> 
<User>...</User>
<User>...</User> 
<Users>

And bind it to an Action like this:

ActionResult CreateUsers(List<User> u) {


Try registering your model binder like this:

ModelBinders.Binders.Add(typeof(List<User>), new SimpleUserBinder());

Note: You won't need the ModelBinderProvider because when you specify the List<User> in your Action it will automatically match the type and call the SimpleUserBinder.

0

精彩评论

暂无评论...
验证码 换一张
取 消