Archive for May, 2013

ASP.NET MVC Html.DropDownList Example

May 24th, 2013

I’ve been struggling for a while to find a decent example of how dropdown lists work in ASP.Net MVC. It seems like a pretty simple thing to do – show a dropdown list with a selected item, allow the user to select a different item then save the new selection back on the server when the user submits the form containing the dropdown. However, it took me a few hours of searching and experimenting to get my dropdown working.

Here’s how I got it working …

1. In the model used to pass data to the view, ensure there are two fields – one containing the list of items to show in the list on the web page, the other field containing the value of the selected item. Here’s a cut down version of my model class:

public class CustomerModelData
    // The list of payment terms. This is a list of strings
    // converted into the SelectList type for use by the
    // Html.DropDownList HTML helper.
    public SelectList PaymentTerms { get; set; }

    // The value of the selected item. Note that this is
    // a string, i.e. the same type as the list members.
    // Note that the name of this field is used by the
    // Html.DropDownList helper later in the example.
    public String PaymentTermID { get; set; }

2. In the controller, when the view is initially loaded, populate the model fields above with the list of items for the dropdown and the value of the selected item.

// Load the data from the database.
CustomerModelData Model = new CustomerModelData();

List<PaymentTermData> DBTerms = DatabaseManager.PaymentTerms;
List<String> DBTermsText = new List<string>();

foreach (PaymentTermData PT in DBTerms)

PaymentTermData SelectedTerm = DatabaseManager.SelectedTerm(); 

// Set the list of items for display converting from a
// list of strings to a SelectList.
Model.PaymentTerms = new SelectList(DBTermsText);

// Set the selected item for the drop down.
Model.PaymentTermID = SelectedTerm.ToString();

3. In the view, plase the Html.DropDownList helper:

@Html.DropDownList("PaymentTermID", Model.PaymentTerms)

Note that there are two parameters to the DropDownList call:

a. “PaymentTermID” – This is the name of the field in the model that contains the selected item.

b.  Model.PaymentTerms – This is the list of items for the drop down.

4. When the user makes a selection on the drop down and then submits the form containing the drop down, e.g. by pressing the Save or Submit button on the form, the same model class (CustomerModelData) can be used to pass data back to the controller:

public ActionResult CustomerDetails(CustomerModelData Data)
    // The value chosen by the user is now held in the model.
    String TheSelectedValue = Data.PaymentTermID;
    // The list of values is NOT passed back to the controller.
    // This field is now null.
    SelectList TheValues = Data.PaymentTerms;

Note that the value selected by the user has been updated in the model BUT that the list of values is now null. If the view is reloaded at the end of the controller action, the list will need to be repopulated in the model.

There may be other, more efficient ways of handling dropdown lists in ASP.Net MVC but this example works for me and took a surprising amount of time and trials to get to a working state. Initially, I had tried to think of the DropDownList helper as though it was a Winforms combo box where I could pass a list of objects, e.g. the actual PaymentTermData class, together with an instance of the class as the selected item. However this didn’t work and in the end I reverted to a simple type (String) for both the list and selected item in the drop down.

Also check out this post for tips on disabling the cache with Ajax requests as I have come across situations where the dropdownlist (and other stuff) appears not to work with the caching enabled.

5 Tips for setting up your business

May 17th, 2013

Once you have your killer idea for a product or service that will fill a proven gap in the market, you’ll need to go about setting up your business. Hopefully you’ll have done your business planning, if you need some tips on this, do look at our other post here.


Here are 5 practical steps you’ll need to consider to actually get the show on the road:

1. Get the name right. Make sure it’s a name that sums up what you offer, either your product or what you want to be known for (your brand values).

There are some useful resources on this website:

2. Register with Inland Revenue for your Tax and National Insurance payments. You can find more information about that, PAYE and importing & exporting here

3. Assuming you already have the finance you need to start your business, you’ll need to keep track of your money. Regularly recording both the money going out (purchases or expenditure) and the money coming in (sales or income). You can hire a book-keeper to do this for you (if you’re the sort who likes to keep your receipts in a carrier bag and hand them over to someone else!). Or you can log them yourself in a spreadsheet or specialised software. Don’t forget, if your business issues invoices, SliQ Invoicing Plus is perfect for start-ups as it’s very quick to set up and simple to use.

4. Get yourself noticed. Think about who your likely customers are and the best methods of reaching them. Promotion could include the more traditional leaflets and local newspapers which would be effective for a local trades person for example. If you go the online / digital route, make sure your website appears high in the Search Engine rankings. You may need to hire a professional to advise you here. Regarding social media, it’s worth considering a training course or again, hiring a professional. There are some great tips on digital marketing here:

5. Tap into one of the many sources of business advice just to make sure you have everything covered. The Federation of Small Businesses (FSB) and the Governments business support provide advice on all aspects of running a business.

For more business startup tips, read

Unable to update the EntitySet because it has a DefiningQuery and no InsertFunction element exists

May 14th, 2013

While working with SQL Server and Entity Framework the following error was raised after adding a new table into the SQL Database and updating the model in my project from the DB:

"Unable to update the EntitySet 'Customers' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation."

The error was raised as an exception on calling the SaveChanges method on the context after adding a new record into a list/ table on the database.

The solution to the problem was simple – although my new SQL table had an ID column, I hadn’t set the column as the primary key. Setting the column as a primary key in SQL Server Management Studio then going back to my Visual Studio project and rebuilding the entity model from the database made the error go away and let me successfully save new records to the table.