locked
Viewbag show in var RRS feed

  • Question

  • User-366177243 posted

    My view index : 

    @{
                foreach(var item in (List<AgendaModel>)ViewBag.ListaEventos)
                {
                     @item.Id,
                     @item.Title, 
                        @item.Start,
                       @item.End,
                        @item.AllDay, 
                        @item.Color, 
                        @item.TextColor;          
                }
    
            }

    This not work but this is one test.

    I have similar this :

    events.push({
                'id': 1,
                'title': 'TESTE1',
                'start': '2020-08-20 00:00:00',
                'end': '2020-08-20 00:00:00',
                'allday': false,
                'color': '#FFFF00',
                'textColor': '#000000'
            });

    Where : 

      '@ViewBag.Id' = id 

    Thanks ! 

    Wednesday, August 19, 2020 1:32 PM

Answers

  • User-474980206 posted

    it is unclear what you are trying to do. while the "@" in razor is used to change to c# mode, in c# its the quote charter for reserved words 

       var @if = 0;

    @{
                foreach(var item in (List<AgendaModel>)ViewBag.ListaEventos)
                {
                        <text>
                        @item.Id,
                        @item.Title, 
                        @item.Start,
                        @item.End,
                        @item.AllDay, 
                        @item.Color, 
                        @item.TextColor;
                        </text>          
                }
    }
    

    will render the values with commas and trailing ;

    if you where trying to initial a javascript array:

    <script>
               @foreach(var item in (List<AgendaModel>)ViewBag.ListaEventos)
                {
                      <text>events.push({
                         id: '@item.Id',
                         title: '@item.Title', 
                         start: '@item.Start',
                        end: '@item.End',
                        allDay: '@item.AllDay', 
                        color: '@item.Color', 
                        testColor: '@item.TextColor';
                        });</text>          
                }
    }
    </script>

    but json serialize would be better

    <script>
      var events = @Html.Raw
      (
           JsonConvert.SerializeObject(ViewBad.ListaEvents, new JsonSerializerSettings
           {
                 ContractResolver = new DefaultContractResolver
                 {
                      NamingStrategy = new CamelCaseNamingStrategy()
                 };
            })
     );
    </script>

    this code makes no sense:

      '@ViewBag.Id' = id 

    if the value of id is 10 then it renders

        10 = id

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 19, 2020 3:17 PM