In my MVC application, I have a service call (http://dev-service.test.com/api/brands?active=true) that returns the following XML
<Brands>
<Brand>
<BrandId>1</BrandId>
<BrandNo>20</BrandNo>
<BrandName>ABC</Domain>
</Brand>
<Brand>
<BrandId>2</BrandId>
<BrandNo>30</BrandNo>
<BrandName>XYZ</Domain>
</Brand>
<Brands>
In one of my user controls, I would like to populate a dropdownlist with the BrandName values. I already have a ViewModel that contains a bunch of properties. How can I populate the dropdown with the values from this XML?
P.S: I am new to MVC and still learning the basics of viewmodels etc.
There really are 2 parts in your question. The XML parsing part (which has strictly nothing to do with ASP.NET MVC) and the ASP.NET MVC part. Since your question is tagged with
asp.net-mvclet’s first answer this part. So you mention a view model. Something like this:then a controller action:
and finally the view part:
Alright, this is where the ASP.NET MVC part ends in your question. Now comes the XML parsing part. There are several ways to parse XML in C#. For example you could use the XDocument class.
Of course before being able to parse XML you need to have XML. What you have shown in your question is not XML. It is a string. You need to first fix it and have a valid XML. Like this:
Now that you have valid XML let’s move on and use a XML parser.
and now let’s make the 2 work together:
This is where we can see that we have strongly coupled out controller action logic with XML parsing logic which is bad. You could introduce an abstraction (interface) which will be injected into the controller’s constructor and then used by the action. Then you could provide a specific implementation of this abstraction that will perform the actual XML parsing and configure your dependency injection framework to pass it to the controller.
So let’s do that. Let’s define a domain model that will represent our brands:
Cool. Now what do we want to do with those brands? Retrieve a list of them. Let’s define our contract:
OK, we have specified what operations we need with our brands. Now we could have our controller look like this:
There is still room for improvement in this controller action. Notice that we are querying the repository and obtaining a list of domain models (
Brand) and converting this domain model into a view model. This is cumbersome and pollutes our controller logic. It would be better to externalize this mapping into a separate layer. Personally I use AutoMapper for that. It’s a lightwight framework which allows you to define the mappings between different classes in a fluent way and then simply pass it instances of the source type and it will spit you instances of the target type:So we are making progress here. Now we could have an implementation of our contract:
And the last step of the puzzle is to configure some DI framework to inject the proper implementation of our contract into the controller. There are like a gazzilion of DI frameworks for .NET. Just pick one. It doesn’t really matter. Try the Ninject.MVC3 NuGet. It’s kinda cool and easy to setup. Or if you don’t want to use third party DI frameworks simply write a custom dependency resolver.