Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey guys, I'm studying to do the MVC4 exam for Microsoft and im running in to a little issue. I'm trying to re-create an old project I done a while back in Web forms in MVC and its proving rather difficult. Ok my problem, I have a bunch of javascript files which look for a div with an id of calender and create a calender control, No matter what I try in MVC I can't get it to work but in Web forms it works like a treat. So I'm trying to access the Calendar div in the Index.cshtml file but no matter what I try I always get underfined

_Layout.cshtml
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Styles.Render("~/Content/jquery-ui.min.css")
    @Styles.Render("~/Content/fullcalendar.css")

    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/Scripts/jquery-2.1.0.js")
    @Scripts.Render("~/Scripts/jquery-ui-1.10.4.js")
    @Scripts.Render("~/Scripts/fullcalendar.js")
    @Scripts.Render("~/Scripts/jquery-ui.custom.min.js")
    @Scripts.Render("~/Scripts/Calendar.js")

</head>


Index.cshtml
<pre lang="xml">@{
    ViewBag.Title = "Calendar";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Calendar</h2>
<div id="calendar"></div></pre>


Calendar.js
   var calendar = $('#calendar');
   var calendar = $('#calendar').fullCalendar({
       header: {
           left: 'prev,next today',
           center: 'title',
           right: 'month,agendaWeek,agendaDay'
},


So in Calendar.js I always get underfined when I try to access the calendar div.

Any help is very much apreciated, bearing in mind that all I have developed in thus far is web forms so if its something silly im missing be nice :).
Posted
Comments
Pravin Patil, Mumbai 2-May-14 19:23pm    
var calendar = $('#calendar');
Is calendar variable in above statement is undefined in your case?
frostcox 2-May-14 19:36pm    
I'm sorry I don't follow, #calendar is declared in the Index.cshtml, I then try access it in the calendar.js file, if you know the correct way I should be declaring it i'm open to suggestions, thanks for your help.

1 solution

You have to load @Scripts.Render("~/Scripts/Calendar.js") inside your Index.cshtml.Remove it from the _Layout.cshtml .Try it.
 
Share this answer
 
Comments
frostcox 3-May-14 7:28am    
Hi thanks for your reply I did as you suggested but I now get the error TypeError: $(...).fullCalendar is not a function. I have the exact same code in a web forms project and it works perfectly. Thanks again for your help.
Sampath Lokuge 3-May-14 7:33am    
That means,your 'fullcalendar.js' is not loaded properly with your _layout page.Please check that with dev tools.
frostcox 3-May-14 8:07am    
Hey all the files are being loaded correctly, I'm just getting undefined when I try access the #calender div now. The code in the Index.cshtml file is @Scripts.Render("~/Scripts/fullcalendar.js")
<br><br><br>
@Scripts.Render("~/Scripts/Calendar.js")<br>
 <br>
<div id="calendar"></div>

Is this correct or am I doing something wrong?
Sampath Lokuge 3-May-14 8:11am    
As an alternative option,You can try like this.Remove 'Calendar.js' and get the code inside that and put it as an inline js withing 'script' tags on index page.Try that way also.
frostcox 3-May-14 8:10am    
The break tags are not in the file thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900