【问题标题】:Adding DateTime in ADO.NET Entity model at MVC在 MVC 的 ADO.NET 实体模型中添加 DateTime
【发布时间】:2013-10-10 06:52:50
【问题描述】:

我使用 ADO.NET 实体模型并检索了数据库的一些表。 然后我创建了一个控制器来添加、编辑、删除表中的数据。 我在表中有一些 DateTime 字段,我想在界面中将其视为日期选择器。 我尝试像这样使用它:

[DataType(DataType.Date)]
public Nullable<System.DateTime> startdate {get;set;}

它没有给我一个日期选择器并继续显示为一个文本框。 如何在 ado.net 模型中使用日期选择器? 无论如何我可以将这些文本框转换为 cshtml 代码中的日期选择器吗?

我是否可以使用 TextBoxFor 而不是 EditorFor 来做到这一点? 我也尝试了 javascript datapicker,也许正确形成它会给我解决方案:

@Html.TextBoxFor(x => x.Baslangic, new { @class = "date" })
@Html.TextBoxFor(x => x.Bitis, new { @class = "date" })

<script type="text/javascript">
    $().ready(function () {
        $('.date').datepicker({dateFormat: "dd/mm/yy"});
    });
</script>

给我一​​个错误提示 “Uncought TypeError:Object [object Object] has no method 'datepicker'”

【问题讨论】:

    标签: jquery jquery-ui asp.net-mvc-4 ado.net datepicker


    【解决方案1】:

    您需要使用类似 jQuery datepicker 的工具来实现这一点。

    看看下面的网站:http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

    更新

    您需要添加一个脚本部分来将这些文本框变成日期选择器。这应该可以帮助您入门,但理想情况下您希望向文本框添加一个类属性,然后只需使用 $('.date').datepicker...

    添加如下内容。

    @Html.TextBoxFor(x => x.Baslangic, new { @class = "date" })
    @Html.TextBoxFor(x => x.Bitis, new { @class = "date" })
    
    <script type="text/javascript">
        $().ready(function () {
            $('.date').datepicker({dateFormat: "dd/mm/yy"});
        });
    </script>
    

    【讨论】:

    • 感谢您的回答。我在创建自己的模型时使用它并且工作正常:[DataType(DataType.Date)] public DateTime ReleaseDate { get;放;但现在我似乎还没有使用 ADO.NET 模型。
    • 查看呈现的 HTML 并了解为什么 jQuery 日期选择器可能不适用于文本框。发布 html 的输出和您的 jquery datepicker 调用。
    • 我更新了我的问题。你知道如何使用 html.textboxfor 吗?
    • 谢谢。我在代码开头添加了这些行。你能帮我把这些应用到 "TextBoxFor"**s?**@Html.TextBoxForModel(x => x.Created, new { @class= "date" }) 是这样吗?
    • 它应该只是 Html.TextBoxFor(x => x...) - 我已经更新了我的答案以进行澄清。
    【解决方案2】:

    知道了! 1- 让用户按照http://jqueryui.com/datepicker/ 的指示进行操作 这是我用cshtml做的代码。把它放在开头

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
      <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css" />
    

    然后输入以下内容

    <script>
        $(function () {
            $('.datepicker').datepicker();
    
        });
    </script>
     @Html.TextBoxFor(x => x.Baslangic, new {@class="datepicker" })
     @Html.TextBoxFor(x => x.Baslangic, new {@class="datepicker" })
    

    【讨论】:

      猜你喜欢
      • 2016-12-11
      • 2018-10-03
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多