如何在MVC 4中使用日期时间控件 [英] How do I use a date time control in MVC 4

查看:82
本文介绍了如何在MVC 4中使用日期时间控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我为学校开发网络应用程序。我需要保存出生日期,入学日期等。我应该从列表中选择日期。如果可能,怎么样?$

我正在编写下面的代码。



在视图中

Hi All,

I developing a web application for a school. I need to save date of birth, admission date etc. Shall I select the date from a list. If possible, how?.
I am writing the code below which I wrote.

In View

 <div class="editor-label">
     <label for="studentDOB">
         Date Of Birth</label>
 </div>
 <div class="editor-field">
     @Html.EditorFor(model => model.studentDOB, new { type = "date" })
     @Html.ValidationMessageFor(model => model.studentDOB)
</div>



在模型中


In Model

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode = true)]
public DateTime studentDOB{ get;  set; }







任何人请帮助我..




Any one please help me..

推荐答案

在MVC中没有对datetime选择器的任何控制。所以你应该使用jQuery UI datepicker或其他一些第三方插件。它很容易实现。



从这里下载jquery插件

http://jqueryui.com/download/ [ ^ ]

然后在头标签中添加Scripts和css,如下所示

In MVC doesn't have any control for datetime picker. So you should use jQuery UI datepicker or some other third party plugin. It's very easy to implement.

Download jquery plugin from here
http://jqueryui.com/download/[^]
and then add the Scripts and css in head tag like below
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>


function (){


#datepicker)。datepicker();
});
< / script>
< / head >
( "#datepicker" ).datepicker(); }); </script> </head>



您还应该更改以下视图代码


Also you should change your view code like below

  <div class="editor-label">
            <label for="studentDOB">
                Date Of Birth</label>
        </div>
        <div class="editor-field">
@Html.TextBoxFor(model => model.studentDOB, new {id="datepicker"})

            @Html.ValidationMessageFor(model => model.studentDOB)
       </div>



希望这有助于


Hope this helps


这篇关于如何在MVC 4中使用日期时间控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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