MVC3剃刀观点错误的Microsoft JScript运行时错误:对象不支持属性或方法“datepicker'` [英] MVC3 razor view error Microsoft JScript runtime error: Object doesn't support property or method 'datepicker'`

查看:3538
本文介绍了MVC3剃刀观点错误的Microsoft JScript运行时错误:对象不支持属性或方法“datepicker'`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个datepicker我Html.EditorFor

I am trying to display a datepicker for my Html.EditorFor

要做到这一点,我在我的意见\\共享\\文件夹EditorTemplate,创建了一个名为EditorTemplate它DateTime.cshtml

To do this I created an EditorTemplate in my Views\Shared\EditorTemplate folder , Named it DateTime.cshtml

但它不工作,而不是我得到的错误--- 微软JScript运行时错误:对象不支持属性或方法日期选择器

but it is not working ,instead I am getting error ---Microsoft JScript runtime error: Object doesn't support property or method 'datepicker'

PLZ帮我解决这个错误

Plz help me to fix this error

这是我的Razor视图页

This is my razor view page

@model MyWebRole.ViewModels.ViewModelEvents
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset style="width:50%">
        <legend>Event Details:</legend>
        <div>
            <table>

                <tr>
                 <td>

                        @Html.EditorFor(model => model.vmStartDate)
                        @Html.ValidationMessageFor(model => model.vmStartDate)
                    </td>


                </tr>
            </table>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

和这里是我的DateTime.cshtml(粘贴以下)

and here is my DateTime.cshtml (pasted below)

@model System.DateTime?

<div class="editor-field">
    @Html.TextBox("", String.Format("{0:MM/dd/yyyy}",(Model == DateTime.MinValue)? null : Model), new { @class="text"})
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#@ViewData.ModelMetadata.PropertyName").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'mm/dd/yy',
            gotoCurrent: true
        });
    });
</script>

我能够得到另一个视图日期选择器的工作

I am able to get datepicker working on another view

  @model MyWebRole.Models.Events
    @{
        ViewBag.Title = "Edit";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset style="width:50%">
            <legend>Event Details:</legend>
            <div>
                <table>

                    <tr>
                     <td>

                            @Html.EditorFor(model => model.StartDate)
                            @Html.ValidationMessageFor(model => model.StartDate)
                        </td>


                    </tr>
                </table>
            </div>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }

两个剃刀视图之间的差别在于
- >第一个((日期选择器不工作)是使用视图模型为模型,即(mywebrole.ViewModels.EventViewModel)

The difference between the two razor views is that -->the first one( (datepicker not working) is using a ViewModel as model ie(mywebrole.ViewModels.EventViewModel)

- >和第二个是使用myWebrole.models.Events(日期选择器工作正常)

--> and the second one is using the myWebrole.models.Events (datepicker working fine)

这是一个问题?

推荐答案

由于错误的,我认为这个问题是选择。代替的 $(#@ ViewData.ModelMetadata.PropertyName)的,试试这个:

Given that error, I think the problem is the selector. Instead of $("#@ViewData.ModelMetadata.PropertyName"), try this one:

$("input.text").datepicker({   ...

(或者,你可以的 @ Html.TextBoxFor(型号=>模型)的代替的 @ Html.TextBox 的,这样的方式的输入将获得的 ID =属性名的,因此,应选择工作)。

(Alternatively, you could @Html.TextBoxFor(model => model) instead of @Html.TextBox, so that way the input would get id=PropertyName, and thus the selector should work).

另外,你一定为脚本的日期选择器的jQuery插件,网页上加载成功,对吧?

Also, you're sure the script for the datepicker JQuery plugin loaded successfully on the page, right?

这篇关于MVC3剃刀观点错误的Microsoft JScript运行时错误:对象不支持属性或方法“datepicker'`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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