0

I am trying to put in a datepicker on a @Html.TextBox. The date field is to be used as a search criteria field so that I can compare a date entry to a date in the table. This is what I have for my scripts:

<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();
    });
});

This is what I have for my TextBox:

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

What it results in is that the words new {@class="getdate"} showing up in the TextBox.

1
  • Try ".getdate" rather than #getdate Commented Jun 29, 2016 at 19:25

5 Answers 5

1

That is because your parameters are messed up for the overloaded method of Html.TextBox..

They should be like this:

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

so with your specific case:

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

Also, in your JS, you are referencing an ID with the #.. but rather you need to reference a class with a ..

$(document).ready(function () {
    $(".getdate").each(function () {
        $(this).datepicker();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

The whole code looks buggy, first of all you are adding a class getdate and in jquery you are using id selector

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

this should be

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

Second thing you are missing value parameter in helper, it should be like this

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

Comments

0

Your order of parameters is not correct. It should be like, first element name, then value and then html attributes. write it this way:

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

See this overload in documentation at MSDN

and now in jquery use class selector, as id should be unique for each element for html:

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

1 Comment

Thank-you everyone. I had tried the .getdate previously however I had the last two parameters in the wrong order. I got my datepicker however my date search still doesn't work.
0

Your last two parameters are in the wrong order

Try:

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

Also your jquery is looking for id=getdate but your html is specifying the class as getdate

Comments

0

You are using incorrect parameter order. It should be:

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

See this overload form:

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

2 Comments

just wondering, wouldn't there need to be null parameter in between the value and the htmlAttributes?
That's just another overload form to accept format parameter. See msdn.microsoft.com/en-us/library/… Although both work why do we need to pass an argument that is null?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.