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
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
@(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
|
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>;
Atanas Korchev
the Telerik team
Is there no way to customize the content when using the binding method?
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
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.
Atanas Korchev
the Telerik team
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.
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>);
Atanas Korchev
the Telerik team
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