locked
foreach Question RRS feed

  • Question

  • User-2051275383 posted

    Hey again guys!

    I am having problem with creating a loop with a javascript for jquery FullCalendar. Have a look below:

    @{
    
        Layout="~/_BlankLayout.cshtml";
        var Today = DateTime.Now.ToString("yyyy-MM-dd");
        var db = Database.Open("db");
        var GrabEvents = db.Query("SELECT * FROM Events");
        
    }
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset='utf-8' />
    <link href="@Href("~/Calendar/Scripts/fullcalendar.css")" rel='stylesheet' />
    <link href="@Href("~/Calendar/Scripts/fullcalendar.print.css")" rel='stylesheet' media='print' />
    <script src="@Href("~/Calendar/Scripts/lib/moment.min.js")"></script>
    <script src="@Href("~/Calendar/Scripts/lib/jquery.min.js")"></script>
    <script src="@Href("~/Calendar/Scripts/lib/jquery-ui.custom.min.js")"></script>
    <script src="@Href("~/Calendar/Scripts/fullcalendar.min.js")"></script>
    <script>
    
    	$(document).ready(function() {
    		
    		$('#calendar').fullCalendar({
    			header: {
    				left: 'prev,next today',
    				center: 'title',
    				right: 'month,basicWeek,basicDay'
    			},
    			defaultDate: '@Today',
    			editable: true,
    			events: 
         [ @foreach(var row in GrabEvents){
         
    	            {
    					title: '@row.text',
    					start: '@row.start_date'
    				} 
    
                    };
    			]
    		});
    		
    	});
    
    </script>
    <style>
    
    	body {
    		margin: 0;
    		padding: 0;
    		font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
    		font-size: 14px;
    	}
    
    	#calendar {
    		width: 900px;
    		margin: 40px auto;
    	}
    </style>
    </head>
    <body>
    
    	<div id='calendar'></div>
    
    </body>
    </html>
    

    I get an error saying CS1012: Too many characters in character literal - I am tipping it doesnt like how I have put the array there.

    Any suggestions on how I can get around this?

    Thanks again!

    Tuesday, August 5, 2014 3:01 AM

Answers

  • User895691971 posted

    Actually you can write the Loop, but the problem that you're just facing here is that you're switiching the content. From client side to server side. You must understand jQuery is a client side language whereas ASP.NET is a server side validation/language. 

    Using the server-side inside the client side is not a good idea. Same is being done here, and the problem you encountered here was the difference between the Character data type and the String data type. Here is the example, 

    // in client side, no char type only var
    var character = 'c';
    var string1 = "Love for all, Hatred for none!";
    
    // both would be rendered as strings. 

    Somehow Server side has some other implementation for it. ASP.NET has following data types

    // var, string, char
    
    var someText = 'c'; // would be a char because compiler would convert var to char in runtime
    char charac = 'c'; // a character as the data type mentions.
    string string1 = "Love for all, Hatred for none!"; // string variable
    
    // but if you do this
    
    char charac = 'Love for all, Hatred for none!'; // too many characters in space of one

    You would get the error. That there are many characters in one char variable. This is the very problem you're facing right now. So, either you can change the qoutation to double qoutes, or your can otherwise convert the context to client side (jQuery) and use the iteration of the JavaScript. But again as I said, you will have to write the entire Database result object to some Client side array to access it. A really long way to go. 

    Other way around would be, to, like create the object that you're creating but in the HTML parse the ASP.NET Database result using the foreach. This way the HTML that would be generated by jQuery would be generated by the ASP.NET itself inside the HTML and not in the script tag. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 6:17 AM

All replies

  • User895691971 posted

    That is because of the fact that you're in the Server-side context. Not inside the client side context. JavaScript allows you to write the Strings in single or double qoutes. But not in Server side. 

    // js
    var string1 = 'Love for all, Hatred for none!'; // Valid
    var string1 = "Love for all, Hatred for none!"; // valid too.

    But in the serverside context, you cannot do this. That is because when you wrote this code

    @foreach(var row in GrabEvents)
    {
        title: '@row.text',
        start: '@row.start_date'
    }

    You switched from Client side to Server side. @ marks the beginning of the Server side ASP.NET Razor Syntax. 

    So, when you're doing that, title and start were expecting a Server side String value. Where as you passed a Characater value. And the Character is only 1 byte long and can only store one char. If you switch the code to this

    @foreach(var row in GrabEvents)
    {
        title: '1',
        start: '1'
    }

    You will see it would work since the single qoutes in Server side are meant for the Char data type. You really need to work out, how would you do this. Otherwise, you can also try to do something like this

    @foreach(var row in GrabEvents)
    {
        title: @row.text,
        start: @row.start_date
    }

    ..by trying to remove the single qoutes from the parameter. 

    Somehow another approach here would be to just use the Client side iteration method and loose your grip from the Server side. But this method doesn't seem to be valid here, since you're using a List of the objects in the server side code and not on the Client side, this method would be a lengthy one. So stick to the first method. 

    Tuesday, August 5, 2014 4:19 AM
  • User-2051275383 posted

    I see what you mean. The jQuery Full Calendar does support JSON but I don't have the knowledge/skill to write what is needed to make that work.. Was hoping to throw a loop in there (as per my OP) but looks like it may not be do-able..

    Tuesday, August 5, 2014 6:07 AM
  • User895691971 posted

    Actually you can write the Loop, but the problem that you're just facing here is that you're switiching the content. From client side to server side. You must understand jQuery is a client side language whereas ASP.NET is a server side validation/language. 

    Using the server-side inside the client side is not a good idea. Same is being done here, and the problem you encountered here was the difference between the Character data type and the String data type. Here is the example, 

    // in client side, no char type only var
    var character = 'c';
    var string1 = "Love for all, Hatred for none!";
    
    // both would be rendered as strings. 

    Somehow Server side has some other implementation for it. ASP.NET has following data types

    // var, string, char
    
    var someText = 'c'; // would be a char because compiler would convert var to char in runtime
    char charac = 'c'; // a character as the data type mentions.
    string string1 = "Love for all, Hatred for none!"; // string variable
    
    // but if you do this
    
    char charac = 'Love for all, Hatred for none!'; // too many characters in space of one

    You would get the error. That there are many characters in one char variable. This is the very problem you're facing right now. So, either you can change the qoutation to double qoutes, or your can otherwise convert the context to client side (jQuery) and use the iteration of the JavaScript. But again as I said, you will have to write the entire Database result object to some Client side array to access it. A really long way to go. 

    Other way around would be, to, like create the object that you're creating but in the HTML parse the ASP.NET Database result using the foreach. This way the HTML that would be generated by jQuery would be generated by the ASP.NET itself inside the HTML and not in the script tag. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 5, 2014 6:17 AM