MVC datetimepicker错误 [英] Mvc datetimepicker error

查看:80
本文介绍了MVC datetimepicker错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@model DateTime
@ Html.TextBox(",Model.ToString("dd/MM/yyyy"),新的{@class ="date"})

我在局部视图类中编写此代码.

在运行用于添加日期时间选择器的mvc剃刀项目时,显示的错误消息如下:.

方法"ToString"的无重载带有1个参数"

我提到了这个网址.
< 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>]

解决方案

我的猜测是Model不是DateTime,因此被视为对象(或其他CLR类型),没有使用参数的ToString()方法的重载.如果Model是DateTime,则应在调用ToString()之前先将其强制转换为DateTime.

希望这对您有帮助


您是否真的将模型值传递给了相应控制器的view方法内的视图?

我已经尝试过这样的代码:

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



并且它在文本框中显示日期时间没有问题.我唯一的问题是文本框的名称不能为空,因此我在其中输入了一些值.
其余的运行正常.


您也可以改用string.Format.这将适用于像DateTime?这样的可为空的类型,也应该适用于对象(前提是该对象支持指定的格式).

因此,您将拥有类似以下内容的信息:

string.Format("{0:dd/MM/yyyy}", Model);



对于此类问题,该替代方法通常是最短的替代方法,我认为在将null用于模型时,该替代方法也将起作用(在不需要初始模型的简单情况下,Create可能会发生这种情况).

@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?

解决方案

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


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.


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).


这篇关于MVC datetimepicker错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆