-1

I'm currently reading ASP.NET MVC Succintly ebook, and some examples aren't working properly, one of them is the following:

I created a project from a ASP.NET MVC 4 (.NET 4.5) Template, and in the model a created this ItineraryItem class:

   public class ItineraryItem {    
       public DateTime? When { get; set; }    
       public string Description { get; set; }    
       public int? Duration { get; set; }    
       public bool IsActive { get; set; }    
       public bool? Confirmed { get; set; }
 }

This is the view:

@model AbacoASP.Models.ItineraryItem
<h2>Create</h2>
<div class="editor">
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        @Html.EditorFor(m=>m)
        <p><input type="submit" value="Save" /></p>
    }
</div>

Then, I created a Custom Editor for DateTime in \Views\Shared\EditorTemplates DateTime.cshtml:

@model DateTime?  
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty),  new { @class = "datePicker" , @readonly = "readonly"})

Then suggest to add this script... but not where. I guess to the same DateTime.cshtml in a script tag:

$(".datePicker").datepicker( 
    {     
        showOn: "button",
        gotoCurrent: true,
        showAnim: 'fold',
        buttonImage: "/Content/calendar.png",
        buttonImageOnly: true 
}) 

There's a similar example that doesn't work either, when I create a Phone.cshtml I get a $ is not defined in console, this is its content:

@model string

@{
    var areaCode = string.Empty;
    var firstNumber = string.Empty;
    var secondNumber = string.Empty;
    if (Model != null) { 
        areaCode = Model.Substring(0, 3); 
        firstNumber = Model.Substring(3, 3); 
        if (Model.Length >= 10) { 
            secondNumber = Model.Substring(6, 4); 
        }
    } 
}
<input type="text" name="area_code" id="area_code" 
       maxlength="3" size="3" value="@areaCode" /> - 
<input type="text" name="number1" id="number1" 
       maxlength="3" size="3" value="@firstNumber" /> - 
<input type="text" name="number2" id="number2" 
       maxlength="4" size="5" value="@secondNumber" /> 
<input type="hidden" name="@ViewData.TemplateInfo.HtmlFieldPrefix" 
       id="@ViewData.TemplateInfo.HtmlFieldPrefix" value="@Model" /> 
<img src="../../../Content/images/Edit32.png" id="phoneNumberEdit" /> 
<input type="text" name="unparsed" id="unparsed" />
<script type="text/javascript">
    $(document).ready(function () {         
        $("#unparsed").hide();         
        var edit = $("#phoneNumberEdit");         
        edit.click(function () { $("#unparsed").toggle(); });         
        var phone = $('#area_code, #number1, #number2');         
        phone.autotab_magic().autotab_filter('numeric');
        $("#unparsed").change(function () {            
            var unparsed = $("#unparsed");            
            var value = unparsed.val();
            value = value.replace(/\(|\)|\s|\-/gi, '');             
            value = value.replace(/[a-zA-Z]+/gi, '');             
            unparsed.val(value);             
            $("#area_code").val(value.substring(0, 3));             
            $("#number1").val(value.substring(3, 6));             
            $("#number2").val(value.substring(6, 10));             
            if (value.length == 10)                 
                unparsed.hide();             
            var result = ($('#area_code').val()                 
                + $("#number1").val()                 
                + $("#number2").val());             
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix")                 
                .val(result);         
        });         
        phone.blur(function () {            
            var result = ($('#area_code').val()                 
                + $("#number1").val()                 
                + $("#number2").val());            
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix")
            .val(result);
        });
    });
</script> 

Edit: The accepted answer in this suggested question doesn't work, and answers aren't helpful at all.

Update: I had to manually add the following to _Layout.cshtml inside :

 @Scripts.Render("~/bundles/jquery")
 @Scripts.Render("~/bundles/jqueryui")

I don't know why jQuery is included in Scripts doesn't work out of the box. Even changed that, the Datepicker is not rendered properly, I got 404 error for the "Calendar.png" and no styling for this.

Community
  • 1
  • 1
Mr_LinDowsMac
  • 2,644
  • 9
  • 56
  • 75
  • 2
    Have you done any research on what `$ is not defined` error is? Without even checking myself, I'm sure there are literally thousands of examples. – Erik Philips Oct 08 '15 at 18:53
  • That error is triggered by the script in Phone.cshtml file at `$(document).ready(function () {` line – Mr_LinDowsMac Oct 08 '15 at 18:55
  • Did you read my comment? – Erik Philips Oct 08 '15 at 18:56
  • Your page needs to be including jQuery and jQueryUI before executing any scripts that reference it. Sounds to me like the tutorials aren't including those. Or if they are, they included jQuery NoConflict, so wrapping your script in `(function($) { /* your script here */ })(jQuery)` should work. If not, include he necessary files. When developing I just use google's hosted libraries https://developers.google.com/speed/libraries/ – Squeegy Oct 08 '15 at 18:58
  • Perhaps I forgot to say that perhaps the problem could be that the script is running before jQuery, to that why fails. I suspected from the beginning – Mr_LinDowsMac Oct 08 '15 at 18:58
  • Do you have anything that looks like this in your code? `` – Luminous Oct 08 '15 at 19:08
  • It is rendering this script after the footer: – Mr_LinDowsMac Oct 08 '15 at 19:13
  • So, jQuery is being loaded, because i'm able two run jquery code snippets from console. – Mr_LinDowsMac Oct 08 '15 at 19:16
  • You never put scripts in `EditorTemplates` or partials. They go in the main view (or its layout). And your 2nd example (`@model string`) will never work because of all the duplicate `id` attributes and invalid html you will create) –  Oct 09 '15 at 00:16

1 Answers1

-1

You have to add two references there : one is for the core library of Jquery and second is for Jquery Ui date picker

http://jquery.com/

https://jqueryui.com/

Varun Vasishtha
  • 461
  • 2
  • 9