Reputation: 148
I want to define a list of selectlist in Razor. The selectlist has four elements, they are current year +1, current year, current year-1 and so on. I want to create this list in client, so I write the code below:
@{
int year = DateTime.Now.Year;
}
<select id="drpYillar">
<option value="1">@year+1 </option>
<option value="2">@year </option>
<option value="3">@year-1 </option>
<option value="4">@year-2 </option>
</select>
The selectlist items appear as 2018+1, 2018, 2018-1 and 2018-2 respectively. How can I appoint them as 2019, 2018,2017,2016?
Thanks in advance.*
Update 1: When I write the question, I have a impression that this is generated at the client side. As Mr Stephen Muecke stated, this als generated at the server side. So question becomes meaningless. Stephen Muecke 's solution at the comment that generating years at the controller is the most rationalist answer.
Upvotes: 0
Views: 35
Reputation: 148
After the warning of Stephen Muecke, generating the razor view that using @ sign anywhere in the razor syntax actually is a server side code, I write client side code to populate years in dropdown menu.
function populateDrpYillar()
{
var currentYear =(new Date()).getFullYear();
//Create array of options to be added
var yearArray= [currentYear -2,currentYear -1,currentYear ,currentYear +1];
var drpYears = document.getElementById("drpYears");
//Create and append the options
for (var i = 0; i < yearArray.length; i++) {
var option = document.createElement("option");
option.value = yearArray[i];
option.text = yearArray[i];
drpYillar.appendChild(option);
}
}
Upvotes: 1