Im trying to convert a value from the dropdownlist in a View in MVC to string format. The dropdownlist contains the value as "01" but when i try to convert it in to string im getting the value as only "1". I want it as how its exactly showing in the dropdownlist i.e "01". Please find my code below. Help appreciated.
NameValueCollection collection = new NameValueCollection();
string startHour = Convert.ToString(collection["combostarthour"]);
I know that i can later convert it in to 01 by the below code but I want it to do while converting itself in the above code. Thanks
string s = startHour.ToString("D2")
Modify your code as,
string startHour = string.Format("{0:D2}", collection["combostarthour"] ?? string.Empty);
which will handle null as well.