0

Posted on : 04-11-2011 | By : Chris | In : Coding, Methodologies, Project Management, Scrum

I love Scrum. For me it just ‘feels’ right and as I’ve never had the luxury of large teams, large budgets and an entourage of experts it is a perfect ‘fit’ to the way I work.

That said, empowered small teams of technical experts with the authority to make decisions is new territory for many organisations and for older, dare I say ‘crusty’ organisations with embedded laboursome processes this is a massive leap of faith!

I find that I often have to justify the merits of Scrum so I thought I’d produce a little blog post that I can refer to – based on the points raised in Ken Schwaber’s excellent book ‘Agile Project Management With Scrum’. Here it is below.

Why Scrum?

  • Scrum doesn’t rely on a complex set of controls (the more complex, the less likely rules and constraints will work). Instead it relies on the judgement of empowered experts – which is a classic way of handling complexity.
  • These experts can make collective and informed judgements to keep the project on track. As such, the team are effectively managing their own delivery.
  • It strips away convoluted communication mechanisms and enables the developers to ‘crack on’ on the customer’s behalf. This is not as risky as you may think for development cycles are short and are therefore far more capable of handling change. There is no better way of learning than through short cycles of discovery. Ken Schwaber refers to this as ‘Fact-based decision making’ which is more effective than ‘Front-end loaded predictive approaches’. (Schwaber, 2004)
  • Each iteration delivers attempts to deliver actual business ‘value’ – there are no partially implemented incomplete features. As such we are left in no doubt as to whether a particular feature really ‘works’.
  • There is a focus on delivering the highest priority first (according to the customer – also called the ‘Product Owner’).
  • There is a ‘test early and often’ ethos to ensure the value ‘vision’ is in line with expectations.

A few things to consider

  • Scrum is beautifully simplistic but don’t let that fool you into a false sense of confidence. Although Scrum excels in its approach to delivering complex changing projects, its lack of controls rely on empowered experts to ensure success. The team must be educated, intelligent and apply common sense for Scrum to be effective!
  • Keep in mind that many of use, especially Project Managers (like me) have been taught, and become used to, various classic project controls such as project plans depicting Gantt charts with milestones and critical paths, resource plans, work schedules, issues and risk logs etc. Throwing these out of the window can feel ‘uncomfortable’ and offer quite a hard sell to an organisation used to the ‘classic way’.

Why did Scrum come about?

  • Software creation is complex. Scrum attempts to keep the process simple.
  • Scrum implements processes to handle this complexity and guide projects to deliver products of value.
  • Software development is prone to change. Scrum welcomes change and implements mechanisms to handle it. In doing so it improves a team’s chances of delivering a successful product.

What is Scrum?

  • An agile approach to delivering complex software (although it can be applied to other project types) projects. It incrementally delivers value through iterative cycles of software development. These cycles continue until the project is shutdown.
  • Each iteration should complete deliver an increment of functionality that is potentially ‘shippable’.
  • Software projects are typically complex so Scrum adopts an ‘empirical process control’ approach
  • An ‘empirical process control’ mechanism is one that collects/manages information by observation, comparison against checkpoint and adaption. In Scrum our checkpoints are the ‘daily scrum’, sprint planning, sprint review and sprint retrospective meetings. At these we consider visible progress against the objective and can modify our approach based on the information collected.
  • At the heart of Scrum’s success sits an empowered team of experts that self-manage and ensure quality through the inspection of each other’s contribution. These experts evaluate the requirements, consider their skills and self-organise to take on the tasks most suitable. The team review progress and adapting their approach regularly if necessary.

Roles

There are only three roles in Scrum. Collectively they manage the delivery.

Scrum Master

The Scrum Master can be considered a, kind of, project manager. The Scrum Master is responsible for providing direction, leadership, advice, knowledge transfer, management (of the development process) and ensuring the team is free from obstacles that may inhibit successful delivery of the product.

Product Owner

The Product Owner ‘owns’ the requirement. It is likely the Product Owner would have been involved in the business case and defining the original requirements. There may also be responsibility for the financials and authorisation for the project to proceed. During the project the Product Owner maintains responsibility for on-going financials and release plans. In Scrum, the requirements list is considered a ‘product backlog’. The Product Owner should organise the product backlog to ensure the highest value products are prioritised and therefore appear top of the list for early inclusion. This list is continually reprioritised with each iteration.

Team

The team deliver the project. They are held jointly responsible for the project and are empowered to structure the list of deliverables (product backlog) and ensure the higher value ‘products’ are delivered first. This list is continually evaluated throughout development.

Pigs and Chickens

The terms ‘Pigs’ and ‘Chickens’ are used in Scrum to describe the commitment of stakeholders. They come from a joke (look it up). In short, Pigs are committed (responsible/accountable) but Chickens are not (they only have an interest). Those responsible (the Pigs) carry the authority required to make the decisions necessary in order to ensure delivery. The chickens, on the other hand, are kept from interfering wherever possible. This is an important feature of Scrum.

The Scrum Approach

The Scrum process begins with a high-level vision (which may be refined over time). The vision ‘belongs’ to the Product Owner who is most likely responsible for ensuring a return on investment from the project.

The Product Owner will create the Product Backlog – a list of functional and non-functional requirements needed to realise the project. These ‘products’ are then prioritised with the most valuable (or highest priority) listed top. It should now be possible to articulate what constitutes a ‘release’ and at what point the project can be considered ‘delivered’. The Product Backlog is continually re-evaluated for each Sprint to accommodate changing requirements and/or capability to deliver against the timeline.

Scrum Process

Scrum uses ‘Sprints’ to deliver incremental ‘value’ over a set period of time (normally 30 consecutive calendar days). To kick-off each Sprint, the Product Owner and Team meet in a ‘Sprint Planning Meeting’. This meeting is time-boxed to 8 hours maximum and used to select products from the Product Backlog for inclusion within the following Sprint. The selection process is guided by what the Product Owner deems the highest value/priority, tempered by what the Team feel they can realistically achieve within the time, based on previous capability.

The Sprint Planning meeting is conducted in two 4 hour parts:

  • Part one: The Product Owner articulates which products have highest value/priority and responds to questions from the Team who seek to better understand the ‘Whats’ and the ‘Whys’. The team then determine what, from the Product Backlog, can realistically be completed within the Sprint. Their commitment, to the Product Owner, is to do their best to deliver against this selection.
  • Part two: The team plan the Sprint by creating an initial set of tasks and activities. This is the ‘Sprint Backlog’ and it will be continually evaluated throughout the Sprint.

The team meet for 15 minutes at the start of each day. This is called the ‘Daily Scrum’ and is used to ensure the members are working productively with no impeding issues. At this meeting three questions are asked by the ScrumMaster:

  1. “What have you done on this project since the last Daily Scrum meeting?”
  2. “What do you plan on doing on this project between now and the next Daily Scrum meeting?”
  3. “What impediments stand in the way of you meeting your commitments to this Sprint and this project?”(Schwaber, 2004)

When a Sprint concludes a ‘Sprint Review’ meeting is held (time-boxed to 4 hours). The team present the new functionality to the Product Owner – other stakeholders are permitted to attend. Collectively the team determine what they should focus on next.
Following the review, there is a Sprint Retrospective meeting (time-boxed to 3 hours) with the Team. The retrospective offers an opportunity to evaluate the Team’s performance within the last Sprint and consider adjustments that improve efficiency for the next Sprint. This meeting must take place before the next Sprint Planning meeting.

Earlier we mentioned that Scrum adopts an ‘empirical process control’ approach. This is achieved through the Sprint planning, Daily Scrum, Sprint Review and Sprint Retrospective meetings.

Bibliography

Schwaber, K. (2004). Agile Project Management With Scrum. Redmond: Microsoft.

http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/digg_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/furl_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/google_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/twitter_48.png

What makes a successful website?

0

Posted on : 24-10-2011 | By : Chris | In : Design, Website

Introduction

Before we start you should know that this post is the first of a series of posts that document my approach to building the ultimate website. It makes sense to start at the beginning and for me that’s the notion of success. Why? Because I want it!

‘Ultimate website’ I say, well isn’t ‘ultimate’ a subjective word? In so much that one person’s ‘ultimate’ is probably another’s person’s ‘mediocre’ and isn’t the same true for the word ‘success’? There’s a saying that suggests (something like) ‘success is measured by what you have sacrificed to achieve it’ – cryptic, I know, but I kind of agree. For me an ultimately successful website is one that excels at all I believe to be good whilst fulfilling the goals I define for it.

This was a hard post to write. Predominantly because I believe all posts should be succinct and to-the-point. It is impossible within a single post to create a list of do’s and don’ts for a successful website. Instead I wanted to take a more considered approach based on the question – for I believe the answer lays within the question itself.

Now, on to the post…

The post!

I’m about to build a website that’ll ultimately (there’s that word again) make me a millionaire…

Want to know how I’m going to do it? Me too!

Actually, I do have some pretty good ideas and some realistic expectations. In fact, in reality it is not the million pounds that’ll signify the website’s success – it’s a bunch of other things. One thing for sure, the more I satisfy my success criteria, the greater the chance of reaching that rewarding pot of gold!

So what does make a website successful?

I think we should establish a few ground rules first!

It is essential that a good, accessible and aesthetically pleasing design is achieved – that’s a given! Poorly designed, shabby websites are as inviting (and give the same impression) as a rundown junkyard of an office block. So don’t skimp, design well! Those of you who think you have great design skills (but probably don’t) be honest with yourself – you’re the one that’ll lose out if your site is ugly, badly laid-out and unusable!

Now the rant is out of the way, let’s get back to the question…

A website is successful when…

…and the answer is…it depends!

What kind of answer is that?!! It’s an honest one. Let’s consider a few scenarios.

1. I want a creds website!

If you are not fussed about gaining new business via the website and simply require a web presence that establishes your credibility then a successful website may involve a few pages about you and your company, the services you offer, some testimonials from happy clients and a mechanism to contact you. You publish your web URL on all of your literature and use word of mouth to encourage prospective clients to ‘check you out’. When they do, WOW you look professional, and you, hopefully, gain a new client. If you pick up any new business as you go it’s a bonus!

2. I want to support my existing business

Here, you may require the website to:
a. offer all of the features of the creds website
b. offer useful information to existing and potential clients (perhaps by way of an extranet) to complement (perhaps ease) existing methods
c. help generate new business.

Innocent looking scenario 2c places us into a complex and dark world of web sales and marketing which is taken to the ultimate level in scenario 3 below.

3. Make me a millionaire website!

In this scenario your website IS your business and all of the above points apply with a massive focus on the generation of new business. To be successful in this arena all the principles of good business apply and more! The obvious stuff is the tip of the iceberg and the foundations go far deeper.

This post is not about the complexities of Internet sales and marketing so I’ll bring it back to the original question.

What makes a successful website?

The answer could be “Achieving whatever you define as success criteria!”

Earlier I suggested I wouldn’t be listing do’s and don’ts. That was a half truth for at its simplest (although I appreciate ‘the devil is in the detail’) here’s a few must do’s (I couldn’t help myself):

  1. Ensure you have absolute clarity as to role your website must fulfil – nothing more, nothing less
  2. Set yourself modest, achievable goals – this is not easy so be sensible
  3. Whatever its role, build it well (good accessible design) to outshine your competition
  4. Ensure you have the tools to monitor your website visitors and viewing trends (get yourself over to Google and sign up for their Google Analytics) activity or you’ll never be able to determine your success
  5. Review and re-plan regularly in a bid to achieve your goals
  6. Unless you have loads of money – in which case you can pay a company to do all this – never stop researching, learning, trying new things and evaluating their success.

Did I mention I’m about to build a website that’ll make me a millionaire (sure I did)?

In the next post I’ll consider my modest and achievable goals for the next 2 years!

http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/digg_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/furl_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/google_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/twitter_48.png

Why I chose Umbraco as my CMS

0

Posted on : 21-10-2011 | By : Chris | In : CMS, Umbraco

Introduction

There comes a point in every person’s life when they need a content management system (CMS)! OK, well, perhaps not every person but chances are if you are reading this you’ve got CMS on the mind (or your seriously lacking excitement in your life!)
With so many different CMS systems to choose from I felt it worth the time investment to consider what I expected and actually needed from a CMS not just today but into the future. This short document details why I chose Umbraco over the myriad of other CMSs available.

Requirements

So, in nutshell here’s what I was looking for:

  1. Runs on Microsoft .NET and utilises SQL Server for its database.
  2. Free! (of course)
  3. Mature (been around the block)
  4. Evidence of confidence – i.e. used by some big companies
  5. Well supported – by a passionate community
  6. Well documented – so that anyone (willing to make the effort) could gain the knowledge needed easily. This included:
    1. Administrators
    2. Users
    3. Developers (to implement extended features)
  7. Simple to implement
  8. Easy to use
  9. Scalable (I think big… It’s a small website today but tomorrow it’ll be a full blown subscription-based money spinner that’ll make me a millionaire!)
  10. Extensible
    1. Through pluggable functionality (i.e. add-ons)
    2. I’m a developer, so I may also wish to extend it myself
  11. When I get really big (next year…) I’ll be far too busy making money to worry about application support so I’ll probably want the whole shebang. I need to know the tool has enterprise level support and monitoring options – perhaps even with the developers themselves!

So what system could deliver against this requirements list?

Blimey you’re good! That’s right! Umbraco ticks all the boxes!

Let’s look at each one briefly.

Runs on Microsoft .NET and Utilises SQL Server for its Database

I actually have two hosting environments. One on Linux (for my WordPress sites like this one) and a Microsoft environment. For my sins I’m a bit of a Microsoft .NET developer and, as mentioned earlier, this new idea of mine will make me millions so there’s a good chance that at some point I’ll want to lift the hood on any CMS I use and implement custom functionality. In any case, I wanted a CMS that was built for a typical Microsoft Environment.

Free!

I’m tight with money – less spend on this means more for gadgets! Enough said!

In reality I just don’t see why I should spend out if something free does everything I need. When I’m a millionaire I may be a little more indulgent.

Having said that the Umbraco team do offer a package called ‘Umbraco Confidence’. This is a good name for the package as it offers a number of reassurance features including:

  • Quality support with a fast response time – within 24 hours
  • Bugs or issues fixed within 7 days by the Umbraco developers
  • General direct help and guidance from the developers
  • Monitoring tools to measure the health and usage of the installation

The ‘confidence’ package also includes enterprise level features such as:

  • Ability to move content and media from test to live site in a simple one-click operation
  • An easy way to design online forms and questionnaires
  • Ability to check-in and check-out documents for exclusive editing.
  • Access to the online training library with in-depth tutorials on Umbraco

I can see a great relationship developing here…

Mature

No new stuff please – God knows what the future has in store for it! I was especially wanting to steer away from any bespoke system or one with minimal adoption. From previous experience bespoke becomes a liability and results in great expense (monetary or time) if you ever need to actually do something new with it or extend it etc. New systems that are not widely adopted can vanish overnight.

Umbraco has been around for 10 years and mainstream for 6+ years. It now has over 85,000 installations (real ones). There is one statement that gives me confidence that Umbraco has a future:

“The Umbraco HQ is privately owned and has been profitable since 2008″

Evidence of Confidence

When I heard that “Take That” use Umbraco I was sold!

…oh, yes, and a small company call Microsoft, and there’s TESCO, Wired.co.uk (that’s the one that really sold it to me…) and a multitude of others.

Well Documented & Supported – by a Passionate Community

Earlier I mentioned that the ‘Confidence’ package offers you direct support by the developers. I also mentioned I was tight with money! So if you’re a cheap skate and looking for a system with plentiful information, online help documentation, support forums and a community (all the free stuff).

Voila!: http://umbraco.com/help-and-support.aspx.

Here you’ll find documentation, training, community, video walkthroughs and directory of pluggable packages. Nice!

Simple to Implement

Enter Microsoft Web Platform Installer! See http://www.microsoft.com/web/downloads/platform.aspx.

If you have direct access to your server or access via RDP then use this little beauty to perform the install for you. If you are interested in running a local copy (for development perhaps) you can install the Web Platform Installer on your local machine and use it to install Microsoft Visual Web Developer and/or Web Matrix, SQL Server Express and IIS 7 and… you guessed it… Umbraco – which will step you through the process, create the site, the database and help promote the perception that you really know what you are doing!

Microsoft Platform Installer Completing the Umbraco Installation

 

Here’s my successful installation (in 10 minutes!). I’m liking the ‘Starter Kit’ approach.

Umbraco successfully installed and showing the Starter Kit options

If you don’t have direct access to your server (I don’t) then I’d recommend the free Microsoft tool WebMatrix (http://www.microsoft.com/web/webmatrix) which will enable you to do the configuration and potentially upload it to your server – a number of hosts offer direct interaction with WebMatrix.

If you used the Web Platform installer (rather than WebMatrix) to install Umbraco you can simply point WebMatrix at the Umbraco installation folder. Here’s an article on the subject: http://www.microsoft.com/web/post/installing-umbraco-with-webmatrix

When it comes to publishing to the live server WebMatrix will attempt to utilise SDF (SQL Compact) database but I use a full blown SQL Server 2008 instance. Umbraco can work with either – just ensure one is created on the server and that you enter the correct connection details. Then, when you first run the Umbraco installation (by accessing the site after you have uploaded the files), it will create the database tables and initial data for you.

Here’s the default install with the nicest one of the four skins! Lovely…

Umbraco Default Install

Easy to Use

Out of the box, with the “Starter Kit”, Umbraco’s interface is unashamedly basic and operates a tree-style menu system where assets are managed. It’s clean, intuitive and ticks my ‘easy-to-use’ box.

Umbraco administration - unashamedly simple

If you’re feeling overwhelmed, the admin area points you to all the learning resources you need. I immediately discovered and downloaded 3 help guides:

  • Getting Started with Umbraco
  • Umbraco-v4—Editors-Manual-v1.0.0
  • Web developer skinning documentation

Aside from actual development, these guides quickly bring you up-to-speed and you’ll be implementing your new design and publishing in no time!

Scalable

It’s a web application so primarily this is determined by your hosting hardware and configuration. There are case studies of Umbraco sites using hundreds of thousands of pages. More info can be found by viewing the case studies at: http://umbraco.com/why-umbraco/case-studies

Extensible

You can download the source code so the potential for Umbraco is only limited by your imagination (and coding skills). Additionally there are many free add-ons available to super-charge your Umbraco CMS!

Popular Free Umbraco Add-ons

There are also some Umbraco developed commercial add-ons that are included in the ‘Confidence Package’ or available at modest prices.

Commercial Umbraco Add-ons

 

So, that’s that!

I’m up and running in no time at all. I have a server installation and a local development version.

Now I just have to figure out how it’ll make me a millionaire – that one’s a little tougher…

Next I’ll be blogging about the choices I make in how I architect and design my Umbraco implementation considering aspects such supporting infrastructure (i.e. dev, staging & live), information architecture, design and features.

Watch out for it soon!

http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/digg_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/furl_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/google_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/twitter_48.png

Use AutoMapper to Map ViewModels to Entities – MVC3 and Razor

0

Posted on : 23-09-2011 | By : Chris | In : MVC

Quite often it is prudent to create ViewModel classes to encapsulate multiple pieces of data (represented by instances of classes) into one easy to manage object that you can pass to your View. This is all well and good but additions and deletes must then  map the data back to the corresponding entities.

  • Mapping code is tedious to write
  • Mapping code is tedious to test

In fact, some of the most boring code in the world to write is mapping between two objects. Enter AutoMapper, described as:

…an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper’s established convention, almost zero configuration is needed to map two types.

See https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

Here’s an uber simple example of it in use:

 

VIEWMODEL
—————-

namespace BenefitsBenchmarkingTool.ViewModels
{
    public partial class CompanyDepartments
    {
        public int CompanyID { get; set; }
        public IEnumerable<SelectListItem> Companies { get; set; }
        public string DepartmentName { get; set; }
        public string DepartmentEmail { get; set; }
    }
}

VIEW
——–

The View then references the ViewModel: @model BenefitsBenchmarkingTool.ViewModels.CompanyDepartments

 

CONTROLLER
—————–

[HttpPost]
public ActionResult Create(CompanyDepartments departmentToCreate)
{
    if (ModelState.IsValid)
    {
        AutoMapper.Mapper.CreateMap<CompanyDepartments, Department>();
        Department newDepartment = new Department();
        AutoMapper.Mapper.Map(departmentToCreate, newDepartment);
        db.Departments.AddObject(newDepartment);
        db.SaveChanges();
    }
    return RedirectToAction("Index", new { id = departmentToCreate.CompanyID });
}

For more information, check out this excellent post: http://lostechies.com/jimmybogard/2009/01/23/automapper-the-object-object-mapper/

http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/digg_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/furl_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/google_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/twitter_48.png

@Html.DropDownListFor SelectedValue in a MVC Razor View

2

Posted on : 23-09-2011 | By : Chris | In : MVC

Although ViewBag is often a quick and dirty way to pass values between the controller and the view be careful not to think of it as your friend. In many (most) occasions this approach is just too simplistic to offer the functionality you require.  More often than not you are best implementing a more robust solution. Auto selecting a dropdownlist value in the view is a perfect example.

If you accept the default code Visual Studio (Express 2010) creates for you when you create a controller from a model “with Read Write Actions and Views” you get something like this:

CONTROLLER
—————–
public ViewResult Index()

        {
            var departments = db.Departments.Include("Company");
            return View(departments.ToList());
        }

VIEW
——–
@Html.DropDownList(“CompanyID”, String.Empty)

 

As you can see quick (very quick – auto generated in fact) but dirty…

The correct approach is to forget the ViewBag and go for a more robust approach using a ViewModel…

 

1. Create a View Model (I placed my in a ‘ViewModel’ folder and called it CompanyDepartments.cs)

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

namespace BenefitsBenchmarkingTool.ViewModels
{
    public class CompanyDepartments
    {
        public int SelectedCompanyId { get; set; }
        public IEnumerable<SelectListItem> Companies { get; set; }
        public string DepartmentName { get; set; }
        public string DepartmentEmail { get; set; }
        public int CompanyID { get; set; }
    }
}

2. Populate the View Model from the Controller - if, like me you have your View Model in it’s own namespace don’t forget to include it in the controller (e.g. using BenefitsBenchmarkingTool.ViewModels;)

public ActionResult Create(int id)
        {
            var companies = db.Companies;

            var model = new CompanyDepartments
            {
                SelectedCompanyId = id,
                Companies = companies.AsEnumerable().Select(x => new SelectListItem
                {
                    Value = x.CompanyID.ToString(),
                    Text = x.CompanyName
                })
            };
            return View(model);

        }

(Interestingly, if I didn’t add ‘.AsEnumerable()’ I got the error LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression)

3. In the View you reference your View Model:

@model BenefitsBenchmarkingTool.ViewModels.CompanyDepartments

Then use the strongly typed DropDownListFor helper:

@Html.DropDownListFor( x => x.SelectedCompanyId, Model.Companies) 
http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/digg_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/reddit_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/dzone_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/delicious_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blinklist_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/blogmarks_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/furl_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/newsvine_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/technorati_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/magnolia_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/google_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/myspace_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/facebook_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://chriscurrie.co.uk/blog/wp-content/plugins/sociofluid/images/twitter_48.png