Set panelbar text with ViewBag value

4 posts, 1 answers
  1. Amy
    Amy avatar
    2 posts
    Member since:
    Sep 2017

    Posted 07 Jan 2019 Link to this post

    Is there a way to set a panelbar individual panel title text with a ViewBag value instead of a literal string?  In my code on line 7, the text is set as "Not Submitted". But I would like to populate it with a string value I build and place in Viewbag that shows a count of records for the type of "Not Submitted". It would read like this - "Not Submitted - (20)".  If it's possible, what would be the syntax to use a variable/constant instead of literal string?

                                         

    01.@(Html.Kendo().PanelBar()
    02..Name("panelbar")
    03..ExpandMode(PanelBarExpandMode.Single)
    04..HtmlAttributes(new { style = "width:100%" })
    05..Items(panelbar =>
    06.{
    07.      panelbar.Add().Text("Not Submitted")
    08.        .Expanded(false)
    09.}
  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5522 posts

    Posted 09 Jan 2019 Link to this post

    Hello Amy,

    You can use "server" code just about anywhere in a view, including in HTML helpers. Here's an example I made for you that shows three examples, and the result is attached below.

    public ActionResult Index()
    {
        ViewBag.myHeaderText = "Not Submitted - (20)";
        ViewBag.myPlainContentText = "lorem ipsum dolor sit amet";
        ViewBag.anotherHeaderData = 33;
        return View();
    }
    @(Html.Kendo().PanelBar()
        .Name("panelbar")
        .ExpandMode(PanelBarExpandMode.Single)
        .HtmlAttributes(new { style = "width:100%" })
        .Items(panelbar =>
            {
                panelbar.Add().Text("Not Submitted")
                    .Expanded(false);
                panelbar.Add().Text(ViewBag.myHeaderText).Content(ViewBag.myPlainContentText);
                panelbar.Add().Text("Not Submitted - (" + ViewBag.anotherHeaderData + ")");
            }
        )
    )


    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Answer
    Amy
    Amy avatar
    2 posts
    Member since:
    Sep 2017

    Posted 09 Jan 2019 Link to this post

    Yes, that is how I've been trying to code it.   And it works if the panel content is just a string.  But I have a kendo grid as the content.  If I set the panel.Text to the Viewbag value, errors appear on the grid code.  The error is "Cannot use lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type."  

    I was able to figure out that I needed a cast to string on the ViewBag value in the index and that fixed the problem. 

    panelbar.Add().Text((string)ViewBag.myHeaderText)

     

  4. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5522 posts

    Posted 10 Jan 2019 Link to this post

    It's good to hear you have this resolved, Amy.

    I am marking your post as the answer to this thread for anyone else having a similar scenario.

    --Marin

Back to Top