Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
@model DateTime
@Html.TextBox("", Model.ToString("dd/MM/yyyy"), new { @class = "date" })

i write this code in a partial view class.

while running my mvc razor project for adding datetime picker an error message is showing like this..

"No overload for method 'ToString' takes 1 arguments"

i reffered this url..
<a href="http://www.programgood.net/2011/03/19/DatePickerAndMVC3.aspx">http://www.programgood.net/2011/03/19/DatePickerAndMVC3.aspx</a>[<a href="http://www.programgood.net/2011/03/19/DatePickerAndMVC3.aspx" target="_blank" title="New Window">^</a>]

Can anyone help me to correct this error?
Posted
Updated 15-Jul-11 0:35am
v4

My guess is that Model is not a DateTime, so is being treated as an object (or other CLR type), which does not have an overload of the ToString() method that takes a parameter. If Model is a DateTime, you should cast it to a DateTime first before calling ToString().

Hope this helps
 
Share this answer
 
Comments
Aswathi Narayan 15-Jul-11 6:17am    
@model DateTime
@Html.TextBox("", Model.ToString("dd/MM/yyyy"), new { @class = "date" })

i write this code in partial view class. But when i execute the program the error is showing like this.."No overload for method 'ToString' takes 1 arguments"
Are you actually passing model value to the view inside action method of the corresponding controller?

I've tried the code like this:

@model System.DateTime
@{
    ViewBag.Title = "CurrentDate";
}
<h2>CurrentDate</h2>
@Html.TextBox("test", Model.ToString("dd/MM/yyyy"), new { @class = "date" })


And it displayed date time inside textbox without problems. The only problem I had was that name of the textbox cannot be empty, so I put some value there.
The rest runs just fine.
 
Share this answer
 
Comments
Aswathi Narayan 15-Jul-11 6:34am    
i need datetimepicker inside the textbox...
i referred this url
http://www.programgood.net/2011/03/19/DatePickerAndMVC3.aspx
skv_lviv 15-Jul-11 7:01am    
Have you tried to go to original post (which is stated at the start of that article) and download sources from it (http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-11-98-72/DatePickerTemp.zip)?
You can also uses string.Format instead. That will works for nullable type like DateTime? and should works for objects too (provide that the object support the specified format).

Thus you will have something like:
string.Format("{0:dd/MM/yyyy}", Model);


For that kind of problem, that alternative is often the shortest alternative and I think it will also works when null is used for the model (which can happen for Create in simple cases when no initial model is required).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900