I am very new to .net MVC. How can I create a list of the last 6 months over the years in .net MVC. The only thing there is is DateTime.Now, and I need
ViewBag.Months=List of months with years
You can create a list of DateTime values using the Enumerable.Range lambda expression. You will need to extract the month / year strings using ToString ("MM / yyyy") for each value in the enumeration. Take a look at this fiddle for a working example: https://dotnetfiddle.net/5CQNnZ
var lastSixMonths = Enumerable.Range(0, 6) .Select(i => DateTime.Now.AddMonths(i - 6)) .Select(date => date.ToString("MM/yyyy"));
That is all you need.
var now = DateTimeOffset.Now; ViewBag.Months = Enumerable.Range(1, 6).Select(i => now.AddMonths(-i).ToString("MM/yyyy"));
Example output (as of February 2016):
01/2016 12/2015 11/2015 10/2015 09/2015 08/2015
now, , . , .
now
Source: https://habr.com/ru/post/1627965/More articles:Implementing the default protocol implementation protocol - iosNested understanding of each cycle - ruby-on-railsКак избежать восстановления глобального индекса Oracle при перемещении раздела - oracledropwizard: read configuration from non-file source - javaTFS Build: `Microsoft.TeamFoundation.PowerShell 'is not installed on this computer - tfsPowerShell (2.0, 32-bit) cannot load the TFS 2010 snap-in ... unless it can - tfsion phonegap-facebook-plugin iOS build - compileC build error - loginhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1627968/block-callbacks-or-protocols-to-pass-information-between-datamanager-and-interactor-in-viper&usg=ALkJrhgXa2B4t2oRJRqV_D5nA7D4TG3GGASQLAlchemy (psycopg2.OperationalError) fe_sendauth: пароль не указан - python401 Unauthorized error sending PUT request to Mailchimp API version 3.0 using Coldfusion / Railo - coldfusionAll Articles