0

I am building an Asp.net MVC app and would like to add a datepicker.

I have this code in my form in the view

@Html.LabelFor(b => b.Date)
@Html.EditorFor(b => b.Date, new {id = "news_date"})

This is the javascript I am using to make a datepicker.

$(document).ready(function () {
    $("#news_date").datepicker({ dateFormat: 'dd/mm/yy' });
});

I would like it so when the user clicks on the textbox a datepicker appears and they can then select a date. currently nothing happens when I click on the text box so I think that there is either an issue with the javascript or with linking it to the textbox. Any ideas?

Edit: Web console shows this error:

TypeError: $(...).datepicker is not a function

Edit: Edit: This is the code that is compiled and taken from the browser console:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Enter Transaction - My ASP.NET MVC Application</title>
        <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.min.css"/>
        <link href="/Content/site.css" rel="stylesheet"/>

        <script src="/Scripts/modernizr-2.6.2.js"></script>


    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title"><a href="/">your logo here</a></p>
                </div>
                <div class="float-right">
                    <nav>
                        <ul id="menu">
                            <li><a href="/">Home</a></li>
                            <li><a href="/Home/About">About</a></li>
                            <li><a href="/Home/Contact">Contact</a></li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">

            <section class="content-wrapper main-content clear-fix">
                <script type="text/javascript">
    $(document).ready(function () {
        $("#Date").datepicker({ dateFormat: 'dd/mm/yy' });
    });
</script>



<h2>Enter Transaction</h2>
<form action="/Home/EnterTransaction" method="post">        <p>
            <label for="Type">Type</label>
            <select id="TypeItems" name="TypeItems"><option value="0">Type1</option>
<option value="1">Type2</option>
</select>
            <br />
            <label for="Date">Date</label>
            <select id="UserItems" name="UserItems"><option value="0">User1</option>
<option value="1">User2</option>
</select>
            <br />
            <label for="Date">Date</label>
            <input type="text" id="news_date" name="news_date" />
            <input class="text-box single-line" data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="Date" name="Date" type="date" value="12/01/2014" />


        </p>
        <input type="submit" value="Submit">
        <p></p>
</form>


            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; 2014 - My ASP.NET MVC Application</p>
                </div>
            </div>
        </footer>
        <script src="/Scripts/jquery-2.0.3.js"></script>



    </body>
</html>
5
  • if you open the browser's console, are there any errors? Commented Jan 12, 2014 at 18:38
  • @mituw16 edited my question with the error i found Commented Jan 12, 2014 at 19:03
  • Have you reference all the required javascript files? Commented Jan 12, 2014 at 19:06
  • @IronGeek could you post an answer with the references that i would need and the locations of those references? Commented Jan 12, 2014 at 19:15
  • @BrianPeach I could, but Raphaël Althaus already did that :) Commented Jan 12, 2014 at 19:25

1 Answer 1

2

Two things :

First, be sure you have references to jquery and jqueryui.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.min.css"/>

You also need jquery ui css referened.

Second, there's no overload of EditorFor taking htmlattributes as parameters.

You should rather do

@Html.LabelFor(b => b.Date)
@Html.EditorFor(b => b.Date)

and for jquery part

$(document).ready(function () {
    $("#@Html.IdFor(m => m.Date)").datepicker({ dateFormat: 'dd/mm/yy' });
});
Sign up to request clarification or add additional context in comments.

7 Comments

I have added the references to jquery and jqueryui into the <head> of the _layout.cshtml, and i have used your jquery method but its still not working. how do i reference the jquery ui css?
@BrianPeach added the jquery ui css. Now, could you say what you have for error in console, if you still have ?
@BrianPeach version for jquery was bad, by the way.
Still getting the same error. I have added the changed to the <head> of the _layout.cshtml with the references and still nothing
@BrianPeach The first for jquery script. Could you post the source code of your page (relevant parts) in a jsFiddle, maybe ? By the way, did you refresh your page (Ctrl + F5) ?
|

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.