This is a migrated thread and some comments may be shown as answers.

[Solved] Set Content on Binding

11 Answers 265 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gabe
Top achievements
Rank 1
Gabe asked on 12 May 2011, 04:21 PM
When I try to set the content while binding I get the following error. I am trying to set the content property for a child item.

CS0136: A local variable named 'item' cannot be declared in this scope because it would give a different meaning to 'item', which is already used in a 'parent or current' scope to denote something else


  @(Html.Telerik().Menu()
        .Name("Menu")
        .BindTo(Model.Menu, mappings => 
        {
            mappings.For<MenuModel>(binding => binding
                    .ItemDataBound((item, menu) =>
                    {
                        item.Text = menu.Name;
                        item.ImageUrl = Url.Content("~/Content/images/cog.png");
                    })
                    .Children(menu => menu.Items));
            mappings.For<MenuItemModel>(binding => binding
                    .ItemDataBound((item, menuitem) =>
                    {
                        item.Content = @<text>
                        <a target="_blank" href="@menuitem.Url">@menuitem.Name</a>
                        
                        </text>;
                    }));
        })
) 

11 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 13 May 2011, 07:21 AM
Hi Gabe,

 There is an implicit variable called @item in Razor hence the problem. You should rename the "item" parameter of ItemDataBound.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Gabe
Top achievements
Rank 1
answered on 13 May 2011, 04:31 PM
So I tried renaming the param as suggested.

 @(Html.Telerik().Menu()
        .Name("Menu")
        .HtmlAttributes(new { style = "width: 110px" })
        .BindTo(Model.Menu, mappings =>
        {
            mappings.For<MenuModel>(binding => binding
                     .ItemDataBound((obj, menu) =>
                     {
                         obj.Text = menu.Text;
                         obj.ImageUrl = Url.Content(String.Format("~/Content/images/{0}", menu.Image));
                         obj.ImageHtmlAttributes.Add("style""vertical-align: middle");
                         obj.HtmlAttributes.Add("style""width: 110px; border: 0;");
                        
                     })
 
                     .Children(menu => menu.Items));
            mappings.For<MenuItemModel>(binding => binding
                    .ItemDataBound((obj, menuitem) =>
                    {
                        obj.Text = menuitem.Text;
                        obj.Url = menuitem.Url;
                        obj.LinkHtmlAttributes.Add("target""_blank");
                        obj.LinkHtmlAttributes.Add("style""text-align: left;");
                        obj.Content = @<text>testing!!</text>;
 
                    }));
        })
)

I get the following Error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1593: Delegate 'System.Action<Telerik.Web.Mvc.UI.MenuItem, Client.MVC.Models.MenuModel>' does not take 1 arguments

Source Error:


Line 11:         .Name("Menu")
Line 12:         .HtmlAttributes(new { style = "width: 110px" })
Line 13:         .BindTo(Model.Menu, mappings =>
Line 14:         {
Line 15:             mappings.For<MenuModel>(binding => binding
0
Atanas Korchev
Telerik team
answered on 16 May 2011, 08:40 AM
Hi Gabe,

The Content setting is used for WebForms style templates. Unfortunately since WebForms and Razor are not compatible with each other you cannot use Content to set the razor template. Hence the compilation error. The proper way to specify the razor template is this:

obj.Template.InlineTemplate = @<text>testing</text>;

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Gabe
Top achievements
Rank 1
answered on 16 May 2011, 03:25 PM
This did not work, it just created an empty drop options basically.
Is there no way to customize the content when using the binding method?

0
Atanas Korchev
Telerik team
answered on 16 May 2011, 04:04 PM
Hello Gabe,

 I tested that before answering the post and it worked as expected. Is there a chance to attach a sample application?

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Gabe
Top achievements
Rank 1
answered on 16 May 2011, 04:22 PM
Here is a small sample project where I am trying to customize the menu items layout.
0
Accepted
Atanas Korchev
Telerik team
answered on 17 May 2011, 06:45 AM
Hello Gabe,

 I see what the problem is. There seems to be a misunderstanding as to what menu content means. Currently the template sets the content of the menu item (its child popup menu) instead of the item inner html. If you want to customize the way the menu item is rendered you have to use the Text property:

.ItemDataBound((obj, menuitem) =>
                    {
                        obj.Text = "<strong>Custom Template Test</strong>" + menuitem.Text;
                        obj.Encoded = false;
                    }));

Setting Encoded to false is required if you intend to use HTML tags. The menu will encode HTML tags unless the Encoded property of the menu items is not set to false.
I am sending you the updated project.

All the best,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
john west
Top achievements
Rank 1
answered on 12 Oct 2011, 09:03 PM
So, two follow up questions to this thread.

1) is there any plan to allow the same kind of templating for the text of the node itself, ie. itm.Text = @<text>@obj.Name</text>;
2) is there any plan to allow these templates to use Razor?

Thanks.
0
Atanas Korchev
Telerik team
answered on 13 Oct 2011, 09:42 AM
Hello John West,

 Do you mean to set the content during data binding or not?

.ItemDataBound((obj, menuitem) =>
 {
                        obj.Text = @<text><strong>item.Text</text>;
                        obj.Encoded = false;
 }));

or 

items.Add().Text(@<text><strong>Foo</strong></text>);

All the best,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Seb
Top achievements
Rank 1
answered on 18 Oct 2011, 11:00 AM
Hi,

that's it, how to set the Content property in PanelBar on data binding ? Should I use the BindTo method or something like "foreach" ?

.Items(item =>
{
        foreach (var chart in Model.Charts)
        {
            item.Add()
            .Text("Chart")
            .Content(() =>
            {%>
                <%=chart.Name%>
            <%});
        }
})

or

.BindTo(Model.Charts, mappings =>
{
    mappings.For<CustomChart>(binding => binding
            .ItemDataBound((item, chart) =>
            {
                item.Text = chart.Name;
                item.Template = chart.Img;
            }));
})

The two examples do not work .. thank you for your help
0
Seb
Top achievements
Rank 1
answered on 18 Oct 2011, 12:18 PM
Problem solved by reading this post
Tags
Menu
Asked by
Gabe
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Gabe
Top achievements
Rank 1
john west
Top achievements
Rank 1
Seb
Top achievements
Rank 1
Share this question
or