ASP.净@Html.文本框Datepicker

本文关键字:Datepicker 文本 @Html ASP | 更新日期: 2023-09-27 17:53:06

我试图在@Html.TextBox上放置一个日期picker。日期字段将用作搜索条件字段,以便我可以将日期条目与表中的日期进行比较。这是我的脚本:

<link href="~/Content/ui_1.10.4_themes_smoothness_jquery-ui.css" rel="stylesheet" />
<script src=" ~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
    $("#getdate").each(function () {
        $(this).datepicker();
    });
});

文本框的设置如下:

Date Received: @Html.TextBox("SearchString5",  new {@class="getdate"}, ViewBag.CurrentFilter as string)

结果是文字new {@class="getdate"}出现在文本框中。

ASP.净@Html.文本框Datepicker

这是因为您的参数对于Html.TextBox的重载方法是混乱的…

应该是这样的:

public static MvcHtmlString TextBox(
    this HtmlHelper htmlHelper,
    string name,
    object value,
    string format,
    object htmlAttributes
)

所以根据你的具体情况:

@Html.TextBox("SearchString5",ViewBag.CurrentFilter as string, new {@class="getdate"})

此外,在您的JS中,您使用#引用ID。而是需要用.引用class

$(document).ready(function () {
    $(".getdate").each(function () {
        $(this).datepicker();
    });
});

整个代码看起来有问题,首先你添加了一个类getdate在jquery中你使用id选择器

$(document).ready(function () {
$("#getdate").each(function () {
    $(this).datepicker();
});
});

应该是

$(document).ready(function () {
    $(".getdate").each(function () {
        $(this).datepicker();
    });
});

你在helper中缺少的第二件事是value参数,它应该是这样的

Date Received: @Html.TextBox("SearchString5",ViewBag.CurrentFilter as string,  new {@class="getdate"})

您的参数顺序不对。应该是这样的,首先是元素名,然后是值,然后是html属性。这样写:

@Html.TextBox("SearchString5",ViewBag.CurrentFilter as string, new {@class="getdate"})

请在MSDN

的文档中查看此重载

和现在在jquery中使用类选择器,因为id应该是唯一的每个元素的html:

$(document).ready(function () {
$(".getdate").each(function () {
    $(this).datepicker();
});

最后两个参数的顺序不对

试题:

Date Received: @Html.TextBox("SearchString5",ViewBag.CurrentFilter as string,  new {@class="getdate"})

同样你的jquery正在寻找id=getdate但是你的html指定类为getdate

您使用的参数顺序不正确。应该是:

@Html.TextBox("SearchString5", "6/30/2016", new { @class="getdate" })

查看重载表单:

https://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.textbox (v = vs.118) . aspx # M: System.Web.Mvc.Html.InputExtensions.TextBox % 28 system.web.mvc.htmlhelper System.String System.Object, System.Object % 29