Can Ürek

Keep walking...

Html.DropDownList selected value not working using ViewBag

The problem may also be the name, see here.

In the Controller

ViewBag.PersonList= new SelectList(db.Person, "Id", "Name", p.PersonId); 

In the View

@Html.DropDownList("PersonList",(SelectList)ViewBag.PersonList )

This will not work, you have to change the name, so it's not the same.

@Html.DropDownList("PersonList123",(SelectList)ViewBag.PersonList )

So change yearDropDown and it Will Work for you.

Best regards Christian Lyck.

http://stackoverflow.com/questions/15686512/html-dropdownlist-selected-value-not-working-using-viewbag

Convert DateTime to Unix time in C#

3
4
5
6
7
8
9
10
11
/// <summary>
/// Convert a date time object to Unix time representation.
/// </summary>
/// <param name="datetime">The datetime object to convert to Unix time stamp.</param>
/// <returns>Returns a numerical representation (Unix time) of the DateTime object.</returns>
public static long ConvertToUnixTime(DateTime datetime)
{
    DateTime sTime = new DateTime(1970, 1, 1,0,0,0,DateTimeKind.Utc);
 
    return (long)(datetime - sTime).TotalSeconds;
}

2
3
4
5
6
7
8
9
10
/// <summary>
/// Convert Unix time value to a DateTime object.
/// </summary>
/// <param name="unixtime">The Unix time stamp you want to convert to DateTime.</param>
/// <returns>Returns a DateTime object that represents value of the Unix time.</returns>
public static DateTime UnixTimeToDateTime(long unixtime)
{
    DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return sTime.AddSeconds(unixtime);
}