Append Text To Drop Down Option Populated from DB

I'm building a page within an MVC application that contains a drop down box that allows the user to select a from a number of financial periods.

For the purposes of making it clearer for our users, I'd like to append "(Current)" to the option that is the current financial period and potentially "(Latest)" to the previous period to represent the last full period. We can already determine which financial period we are in and subsequently the one previous and so on.

So let's say the two options I'm looking to append to currently look like this:

  • Period 11
  • Period 12

and I'd like it them to read:

  • Period 11 (Latest)
  • Period 12 (Current)

The drop down is populated from a database, but I would like to append this text at the application level.

Is this something that is possible? If so, how? I'm at a bit of a loss as to how to go about it.

Thanks for any help in advance.

Answers


I assume you're using some sort of IEnumerable on your model/view model class to populate the items in the drop-down. When you construct this list, simply add your "(Current)" and "(Latest)" options before returning the model to your view. If FinancialPeriod is a model/view model class like this:

public class FinancialPeriod{
    public int FinancialPeriodId { get; set; }
    public string DisplayText { get; set; }
}

Then you could do this in your controller, assuming that your list of all Financial Periods retrieved from the database is a property on your model called FinancialPeriods:

public ActionResult Index(){
    var model = GetTheModel();
    model.FinancialPeriods.Add(new FinancialPeriod{ 
        FinancialPeriodId = <something>, 
        DisplayText = "(Current)"
    };
    model.FinancialPeriods.Add(new FinancialPeriod{
        FinancialPeriodId = <somethingElse>
        DisplayText = "(Latest)"
    };
    return View(model);
}

Make sure you substitute values for <something> and <somethingElse> that you can interpret correctly when the model is posted back to your controller.

Edit - based on your comments and edits, something like this is more appropriate:

public ActionResult Index(){
    var model = GetTheModel();
    var currentMonth = DateTime.Now.Month;
    var previousMonth = currentMonth - 1;
    if (previousMonth == 0)
        previousMonth = 12;
    var currentPeriod = "Period " + currentMonth.ToString();
    var latestPeriod = "Period " + previousMonth.ToString();
    var newList = new List<FinancialPeriod>();
    foreach(var period in model.FinancialPeriods){
        if(period.DisplayText == currentPeriod)
        {
            newList.Add(new FinancialPeriod
                { 
                    FinancialPeriodId = period.FinancialPeriodId,
                    DisplayText = period.DisplayText + " (Current)"
                };
                continue;
        }
        if(period.DisplayText == latestPeriod) 
        {
            newList.Add(new FinancialPeriod
                {
                    FinancialPeriodId = period.FinancialPeriodId, 
                    DisplayText = period.DisplayText + " (Latest)"
                };
            continue;
        }
        newList.Add(period);
    }
    model.FinancialPeriods = newList;
    return View(model);
}

Need Your Help

Converting a querystring into an associative array

php query-string

In PHP, I need a function to convert a querystring from an URL, say: http://example.com?key1=value1&amp;key2=value2 into a PHP associative array : array ['key1' =&gt; 'value1', 'key2' =&gt; 'value2...

Resource interpreted as Script but transferred with MIME type text/plain - for local file

javascript html google-chrome mime-types

I'm getting a "Resource interpreted as Script but transferred with MIME type text/plain" warning in Google Chrome when including a local script file.