0

奇怪的是,我没有收到错误消息,但是我正在使用的 jquerydatepicker函数和numeric插件在它们应该附加到的任何一个输入字段上都不起作用。

这只是一个简单的创建页面,因此控制器方法只会将用户重定向到该Create.cshtml页面。

    //
    // GET: /ZipCodeTerritory/Create

    public ActionResult Create()
    {
        return View();
    }

我只是从这个页面开始,所以视图同样简单。Create以下是页面后跟共享布局中的 jquery 脚本标签。

布局

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/typeahead.js")" type="text/javascript"></script>

创建视图

@model Monet.Models.ZipCodeTerritory

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

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.numeric.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/datepicker.css")" rel="stylesheet" type="text/css" />

<script type="text/javascript">
    $('#ZipCode').numeric();
    $('#EndDate').datepicker({
        dateFormat: 'mm/dd/yy'
    })
    $('#EffectiveDate').datepicker({
        dateFormat: 'mm/dd/yy'
    })
</script>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ZipCodeTerritory</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.ChannelCode)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.ChannelCode, new { maxLength = 1 })
            @Html.ValidationMessageFor(model => model.ChannelCode)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DrmTerrDesc)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.DrmTerrDesc, new { maxLength = 30 })
            @Html.ValidationMessageFor(model => model.DrmTerrDesc)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.IndDistrnId)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.IndDistrnId, new { maxLength = 3 })
            @Html.ValidationMessageFor(model => model.IndDistrnId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.StateCode)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.StateCode, new { maxLength = 2 })
            @Html.ValidationMessageFor(model => model.StateCode)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ZipCode)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.ZipCode, new { maxLength = 9 })
            @Html.ValidationMessageFor(model => model.ZipCode)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EndDate)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.EndDate)
            @Html.ValidationMessageFor(model => model.EndDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EffectiveDate)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.EffectiveDate)
            @Html.ValidationMessageFor(model => model.EffectiveDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastUpdateId)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.LastUpdateId, new { maxLength = 8 })
            @Html.ValidationMessageFor(model => model.LastUpdateId)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这是呈现的 HTML

<script type="text/javascript">
    $('#ZipCode').numeric();
    $('#EndDate').datepicker({
        dateFormat: 'mm/dd/yy'
    })
    $('#EffectiveDate').datepicker({
        dateFormat: 'mm/dd/yy'
    })
</script>

<form action="/ZipCodeTerritory/Create" method="post">    <fieldset>
        <legend>ZipCodeTerritory</legend>

        <div class="editor-label">
            <label for="ChannelCode">Channel Code</label>
        </div>
        <div class="editor-field">
            <input id="ChannelCode" maxLength="1" name="ChannelCode" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="ChannelCode" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="DrmTerrDesc">DRM Territory Description</label>
        </div>
        <div class="editor-field">
            <input id="DrmTerrDesc" maxLength="30" name="DrmTerrDesc" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="DrmTerrDesc" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="IndDistrnId">Territory</label>
        </div>
        <div class="editor-field">
            <input id="IndDistrnId" maxLength="3" name="IndDistrnId" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="IndDistrnId" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="StateCode">State Code</label>
        </div>
        <div class="editor-field">
            <input id="StateCode" maxLength="2" name="StateCode" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="StateCode" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="ZipCode">Zip Code</label>
        </div>
        <div class="editor-field">
            <input id="ZipCode" maxLength="9" name="ZipCode" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="ZipCode" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="EndDate">End Date</label>
        </div>
        <div class="editor-field">
            <input data-val="true" data-val-required="The End Date field is required." id="EndDate" name="EndDate" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="EndDate" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="EffectiveDate">Effective Date</label>
        </div>
        <div class="editor-field">
            <input data-val="true" data-val-required="The Effective Date field is required." id="EffectiveDate" name="EffectiveDate" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="EffectiveDate" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="LastUpdateId">Last Update ID</label>
        </div>
        <div class="editor-field">
            <input id="LastUpdateId" maxLength="8" name="LastUpdateId" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="LastUpdateId" data-valmsg-replace="true"></span>
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
</form>
<div>
    <a href="/ZipCodeTerritory">Back to List</a>
</div>

就像我上面说的那样,EndDateEffectiveDate字段在单击时都不会显示日历,您可以在ZipCode输入框中输入任何您想要的值。希望这不是什么太复杂的事情。

编辑

所以看起来根本没有 jquery 插件工作。添加了以下标签,也无法.mask正常工作。这些是Create视图中的脚本声明

    <h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.numeric.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/datepicker.css")" rel="stylesheet" type="text/css" />  
4

1 回答 1

0

通过将这些放入document.ready函数中,它们现在可以工作了。

$(document).ready(function() {
    $('#ZipCode').numeric();
    $('#EndDate').datepicker({
        dateFormat: 'mm/dd/yy'
    })
});
于 2013-10-04T14:56:08.387 回答