jQuery的DatePicker的MVC4 EditorFor [英] jQuery DatePicker MVC4 EditorFor

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

问题描述

我需要在一个EditorFor场使用日期选择器。

我的code的看法是:

 < D​​IV CLASS =编辑标记>
    @ Html.Label(生日,生日)
< / DIV>
< D​​IV CLASS =主编场>
    @ Html.EditorFor(型号=> model.birthDate,新{@class =日期选择器})
    @ Html.ValidationMessageFor(型号=> model.birthDate)
< / DIV>

脚本:

 <脚本类型=文/ JavaScript的>
$(文件)。就绪(
    功能(){
        $('。日期选择器)。日期选择器({
            changeMonth:真实,
            changeYear:真实,
            MINDATE:-99Y
            DATEFORMAT:DD / MM / YYYY
        });
    });

我不能使它工作。我有以下错误:Microsoft JScript运行时错误:对象不支持此属性或方法日期选择器

我已经加载的jQuery之前,我尝试调用该函数。


解决方案

  @ Html.EditorFor(型号=> model.birthDate,新{@class =日期选择器})

就不行,因为EditorFor不接受(以及传递给DOM)的 A级

所以,您需要使用TextBoxFor将接受一个类或更好的是,创建一个EditorTemplate ,以应付这是一个DateTime任何领域,并添加类那里。 此链接详细介绍了如何使模板。

一旦你的工作类(可以在源$ C ​​$ C进行验证),剩下的只是引用了jQuery code。我用

  @ Styles.Render(〜/内容/主题/基/ CSS)
@ Scripts.Render(〜/包/ jqueryval)
@ Scripts.Render(〜/包/ jQueryUI的)

和与你已有的脚本激活。手表名称为.datepicker'是完全一样的类。

I need to use datepicker on an EditorFor field.

My code for the view is:

<div class="editor-label">
    @Html.Label("birthDate","birthDate")
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.birthDate, new { @class = "datepicker"})
    @Html.ValidationMessageFor(model => model.birthDate)
</div>

The script:

<script type="text/javascript">
$(document).ready(
    function () {
        $('.datepicker').datepicker({
            changeMonth: true,
            changeYear: true,
            minDate: "-99Y",
            dateFormat: "dd/mm/yyyy"
        });
    });

I can't make it work. I have the following error: Microsoft JScript runtime error: Object doesn't support this property or method 'datepicker'.

I have already loaded jQuery before I try to call the function.

解决方案

@Html.EditorFor(model => model.birthDate, new { @class = "datepicker"})

Will not work, because "EditorFor" will not accept (pass along to the DOM) a Class.

So, you need to use "TextBoxFor" which will accept a Class or better yet, create an EditorTemplate to handle any field that is a DateTime and add the class there. This link details how to make the Template.

Once you have the Class working (can be verified in the source code), the rest is just referencing the jquery code. I use

@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")

and activating it with the script you already have. Watch that the name '.datepicker' is Exactly the same as the Class.

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

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