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

[Solved] Foreign Key won't work

1 Answer 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Salvador
Top achievements
Rank 1
Salvador asked on 23 Apr 2012, 10:09 PM
Hi, everybody I'm new using this controls and I can't find the way to make a foreignkey column to show a dropdown on editmode, this is my controller.

namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        public PortalRepository entities = new PortalRepository();
 
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
 
            ViewData["modulos"] = entities.GetAllModules().ToList();
 
            return View();
        }
 
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult GridUpdate( int rolID)
        {
            var rol = new RolesModulo
            {
                RolID = rolID
            };
 
            if (TryUpdateModel(rol))
            {
                entities.UpdateRol(rol);
 
                //return RedirectToAction("Index", this.GridRouteValues());
            }
            //return View("Index", entities.GetAllRoles());
            return View(new GridModel(entities.GetAllRoles()));
        }
 
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult GridInsert()
        {
            var rol = new RolesModulo();
 
            if (TryUpdateModel(rol))
            {
                entities.AddRol(rol);
                //return RedirectToAction("Index", this.GridRouteValues());
            }
 
            //return View("Index", this.GridRouteValues());
            return View(new GridModel(entities.GetAllRoles()));
        }
 
        [GridAction]
        public ActionResult GridSelect()
        {
            return View(new GridModel(entities.GetAllRoles()));
        }
 
        public ActionResult About()
        {
            return View();
        }
    }
}

And this is the view

@using Telerik.Web.Mvc.UI;
@using MvcApplication3.Models;
@using System.Collections;
@{
    ViewBag.Title = "Home Page";
}
 
<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc<;/a>.
</p>
@(
 Html.Telerik().Grid<RolesModulo>("roles")
        .Name("grid")
        .DataKeys(k=> k.Add(o=> o.RolID))
        .ToolBar(c=>c.Insert().ButtonType(GridButtonType.Image))
        .Columns(c =>
        {
            c.Bound(o => o.RolID).Hidden();
            c.Bound(o => o.Activo);
            c.Bound(o => o.Nombre);
            c.Bound(o => o.Descripcion);
            c.ForeignKey(o => o.ModuloID, (IEnumerable)ViewData["modulos"], "ModuloID", "Nombre").Title("Módulo");
            c.Command(s =>
            {
                s.Edit();
            });
        })
        .DataBinding( b=> b.Ajax()
            .Select("GridSelect","Home")
            .Insert("GridInsert", "Home")
            .Update("GridUpdate", "Home")
            )
        .Pageable()
        .Sortable()
        .Filterable()
        .Groupable()
)

I have tried to make it work with Ajax and Server Binding. I can't figure why shows the correct info on browse mode, but won't show the dropdown.

Any help would be deeply appreciated.

1 Answer, 1 is accepted

Sort by
0
Salvador
Top achievements
Rank 1
answered on 24 Apr 2012, 06:00 PM
Nevermind, I found that the Editor Templates were missing, I just copied them from the Demos app and everything work.

Thanks anyway.
Tags
Grid
Asked by
Salvador
Top achievements
Rank 1
Answers by
Salvador
Top achievements
Rank 1
Share this question
or