Reputation: 1087
Controller's Code
int No= Convert.ToInt32(reqCookies["NO"].ToString());
Student stud = db.Students.Single(n => n.No == No);
return View(stud);
Im passing the No after the user logs In and want the record of that logged In person to be displayed which i have successfully done.My issue is again with the Date.When the record of the Logged In user is displayed the Date of birth is displayed along with the time.Like this 2/19/2001 12:00:00 AM, instead i only want the date part and not time something like this 19/2/2001. To achieve this i have tried to convert the date in dd/MM/yyyy format
DateTime DOB=stud.DOB.tostring("dd/MM/yyyy");
Getting Error:No Overload for method 'ToString()' takes 1 Argument
Code in my View is like this:
<td>@Html.DisplayFor(model => model.DOB.ToString())</td>
And when i try to change the format in the View
<td>@Html.DisplayFor(model => model.DOB.ToString("dd/MM/yyyy"))</td>
Getting Error:No Overload for method 'ToString()' takes 1 Argument
Where can i make the conversion and how to truncate the time Part from getting displayed.
Upvotes: 4
Views: 9804
Reputation: 4909
You could use DisplayFormat in the model to format your date:
[DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}")]
public DateTime myDate { get; set; }
And then in the view:
<td>@Html.DisplayFor(model => model.DOB)</td>
Update:
Alternatively, since the DisplayFor() will simply output the date without any tags, you could just display the date without the use of templated helpers:
@Model.myDate.ToString("dd/MM/yyyy")
Or to display in a textbox you could use:
@Html.TextBox("Date", Model.myDate.ToString("dd/MM/yyyy"))
Upvotes: 5