Skip to main content

Using XLinq & MVVM to Populate DDL for ASP.NET MVC

Making some adjustments to a website recently. It's not a heavily used beast, but my client required some dynamic functionality - pulling data from a semi-static data-source that he would be able to edit. Hooking up a SQL Server back-end would have been overkill, so I looked into XLinq as an alternative. I have tinkered with XLinq in the past but haven't ever found any particularly useful application for it. This time however it worked out beautifully.

Setup an MVVM for the page that I was working with and used a technique for loading the data that admittedly I need to credit to Sascha Barber. The technique is fantastic and works perfectly.


Here's my data, stored in a file name things.xml at http://things.com/things.xml:


<?xml version="1.0" encoding="UTF-8" ?>
<things>
<thing>
<name>-- STUFF --</name>
</thing>
<thing>
<name>THING ONE</name>
</thing>
<thing>
<name>THING TWO</name>
</thing>
...
</things>


Here's the MVVM:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.Xml.Linq;

namespace ThingSite.Controllers.ViewModels
{
public class ThingsViewModel
{
...
public SelectList Things { get; private set; }
public Thing CurrentThing { get; private set; }

// constructor
public ThingsViewModel()
{
Things = new SelectList(GetThings(), "Text", "Value");
...
}

private IEnumerable GetThings()
{
string fullXmlPath = "http://things.com/things.xml";

var xmlThingResults =
from thing in Common.StreamElements(fullXmlPath, "thing")
select new SelectListItem
{
Text = thing.Element("name").Value,
Value = thing.Element("name").Value,
Selected = false
};

var list = xmlThingResults.ToList();
return list;
}
...


Here's the Common.StreamElements() static method (thanks Sascha!):


public static IEnumerable StreamElements(string uri, string name)
{
using (XmlReader reader = XmlReader.Create(uri))
{
reader.MoveToContent();
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == name))
{
XElement element = (XElement)XElement.ReadFrom(reader);
yield return element;
}
}
}
}


Here's the code that returns the MVVM from the Controller:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThingSite.Controllers.ViewModels;

namespace ThingSite.Controllers
{
public class ThingBookingController : Controller
{
...
//
// GET: /ThingBooking/Create

public ActionResult Create()
{
return View(new ThingsViewModel());
}
...


And here's how it's used in the View to populate a DDL:


<%@ Page Title=""
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<ThingSite.Controllers.ViewModels.ThingsViewModel>" %>

...
<% Html.EnableClientValidation(); %>
...

<td colspan="3">
<%:
Html.DropDownListFor(m => m.CurrentThing, Model.Things)
%>
<span class="validation"><%= Html.ValidationMessageFor(m => m.CurrentThing) %></span>
</td>
...


So providing he more-or-less knows how to edit an XML file (he does!) my client is able to go into the XML file and adjust the list of things available for the DDL to be populated by. I reckon that's a pretty nifty technique, for lightweight use.

Comments

Popular posts from this blog

HOW-TO: Apply a “baseless merge” in Team Foundation Server 2010 (and 2012)

Another purely technical post on TFS...
The scenario We wish to migrate code between branches that do not have a branch/merge relationship, in order to expedite urgent changes being made by a project team, without disrupting on-going BAU development work. Sample branch hierachy/strategy Imagine the following branching strategy in TFS (visible by connecting to TFS via Visual Studio 2010 or 2012):

Essentially you have a "DEV" branch, which has a "QA" branch, which in turn has a "PROD" branch. DEV is the branch that you would be using for BAU development. As a piece of development matures, you move it into QA, where it is tested by your internal QA team. There may be further changes made in DEV that are moved into the QA branch as the QA team pick up issues. Once the QA team are happy with a packaged of changes, they will move them into PROD, which is essentially the hand-over to the customer. The PROD branch represents the software that the customer has.

Mo…

HOW-TO: Add/edit a field in Team Foundation Server 2012 using Visual Studio 2012

It's been a while since I made a purely technical post...

So, today I wanted to make a change to a Microsoft Team Foundation Server 2012 (TFS2012) instance that I am working with to reflect "Actual" time spent on a task - mainly for reporting purposes, and because I have found in the past that making this minor process adjustment yields a relatively useful metric over the long-term.

I am using the Microsoft Scrum 2.1 Process Template (http://msdn.microsoft.com/en-us/library/vstudio/ff731587.aspx) for a project that I am working with. So that I don't forget how to do this (again!) I will blog-post the procedure I've used to add this field to the template as a screen-shot-based tutorial, as follows...
Before Assuming you are familiar with the Scrum Process Template (2.1-ish) - open a task and take a look at the "Details" section, as follows:


 This is where I want my "Actual" field to show up.
Get the Power Tools Download and install the latest v…

Eclipse/Android error: "Multiple dex files define [...]"

Wow, I am really going nuts blogging this-evening - 2nd post in less than an hour. 


Anyway this is a particularly nasty error that I keep running into with Eclipse/Android when starting the emulator after I have not run it for a little while. Since I run the risk of permanently forgetting the solution to the problem every time I walk away from my Android project (and thus having to spend a painful hour-or-so digging up the procedure again), I will blog it here, for my benefit, and for the benefit of anyone who may also suffer the same problem.


The gist is that when you start the emulator in debug mode (that is, you hit the button in the following image), you get the following error message come out on the console and a nasty popup telling you nothing more than there is an error with your program and you need to fix it:








[2012-04-06 23:20:57 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/gson/ExclusionStrategy;
[2012-04-06 23:20:57 - SimpleList] Conversion to Dal…