MVC Telerik PanelBar problèmes avec BindTo
Bonjour,
Voilà, j'utilise telerik pour afficher un panelbar dans lequel je souhaite tout simplement afficher des items regroupés par catégories. (mes items sont des containers). Cependant je n'arrive pas à utiliser le code fournit par telerik (qui utilise une méthode BinTo) dans sa démo.
J'utilise aussi Linq To SQL comme "ORM".
J'ai fini par utiliser des boucles un peu crades et j'ai l'intime conviction que mon code n'est pas très "propre".
Voici le code telerik :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Html.Telerik().PanelBar()
.Name("PanelBar")
.BindTo(Model, mappings =>
{
mappings.For<Category>(binding => binding
.ItemDataBound((item, category) =>
{
item.Text = category.CategoryName;
})
.Children(category => category.Products));
mappings.For<Product>(binding => binding
.ItemDataBound((item, product) =>
{
item.Text = product.ProductName;
}));
}) |
et voici mon code (qui marche mais n'est pas optimisé ama)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <% Html.Telerik().PanelBar()
.Name("PanelBar")
.HtmlAttributes(new { style = "width: 200px; float: left; margin-bottom: 30px;" })
//.ExpandMode((PanelBarExpandMode)Enum.Parse(typeof(PanelBarExpandMode), (string)ViewData["expandMode"]))
.ExpandMode(PanelBarExpandMode.Multiple)
//.SelectedIndex(0)
.Items(item =>
{
foreach (var o in (IEnumerable<SelectListItem>)ViewData["ContainerCategoryList"])
{
item.Add()
.Text(o.Text)
//.ImageUrl("~/Content/PanelBar/Container/Box_Empty.png")
.ImageHtmlAttributes(new { alt = o.Value.ToString() })
.Items(subItem =>
{
foreach (var o2 in Model)
{
if (int.Parse(o.Value) == o2.lCategory)
{
subItem.Add()
.Text(o2.Identification);
}
}
});
}
})
.Render(); %> |
et coté controller j'ai ça :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public ActionResult Index()
{
RegisterLists();// Container Categories
IEnumerable<ContainerCategory> enumCat = new Repository<ContainerCategory>(_Repository.Context).GetAll();
return View(_Repository.GetAll());// Repository pointe déjà sur l'entité Container du Domain (cf constructeur)
}
void RegisterLists()
{
// Passe au ViewData les catégories de contenants
ViewData["ContainerCategoryList"] =
new SelectList(new Repository<ContainerCategory>(_Repository.Context).GetAll(), "lContainerCategoryId", "Name");
} |
Est ce que je passe les bons objets à la vue ? Sauriez vous me dire si c'est optimisable. Je ne sais pas si le code de démo de telerik est prévu pour EF ou linq2sql ou si ça n'a pas d'importance.
MErci pour votre aide.