Foreign key dropdown depending on content

3 posts, 1 answers
  1. Leo
    Leo avatar
    19 posts
    Member since:
    Jan 2016

    Posted 06 Apr 2016 Link to this post

    Hi, as i'm new to these forums, i hope you will not be to hard on me if the solutions is obvious :-)

    Let's say i have setup this grid with Products including a foreign key dropdownlist to ProductTypes. Some of these ProductTypes are no longer in use (marked boolean property 'IsValid')
    The requirement is that all Products with ProductType are shown independent of the 'IsValid property'. So far so good.
    Now the point where i need some help: when a record is added, i would like the ProductType dropdown to only list ProductTypes that are marked as valid. And to make it even more difficult: When a row is in edit mode, i would like the dropdown to only show the valid ProductTypes AND the current ProductType even if that is invalid.
    public class ProductController : Controller
            private DBEntities db = new DBEntities ();
            public ActionResult Index()
                ViewData["ProductTypes"] = db.ProductTypes.Select(b => new { Id = b.idProductType, Name = b.ShortName });
                return View();

          .Columns(columns =>
              columns.Bound(c => c.Code);
               columns.Bound(c => c.Color);
              columns.ForeignKey(c => c.idProductType,
    (System.Collections.IEnumerable)ViewData["ProductType"], "Id", "Name").Title("Type");
              columns.Command(command => { command.Edit(); command.Destroy(); }).Width(180);
          .ToolBar(toolbar => {
          .Editable(editable => editable.Mode(GridEditMode.InLine))
          .Sortable(sortable => {
          .Scrollable(scrollable => scrollable.Enabled(false))
          .DataSource(dataSource => dataSource
              .Model(model => model.Id(p => p.idProduct))
              .Read(read => read.Action("KProduct_Read", "Product"))
              .Create(create => create.Action("Product_Create", "Product"))
              .Update(update => update.Action("Product_Update", "Product"))
              .Destroy(destroy => destroy.Action("Product_Destroy", "Product"))
  2. Leo
    Leo avatar
    19 posts
    Member since:
    Jan 2016

    Posted 07 Apr 2016 in reply to Leo Link to this post

    To rephrase the question: How can you provide a different dataset to the foreignkeycolumn for add / edit / read mode?
  3. Answer
    Nikolay Rusev
    Nikolay Rusev avatar
    2289 posts

    Posted 08 Apr 2016 Link to this post

    Hello Leo,


    You can easily achieve this by defining remote binding for the foreignkey column editor instead. Thus on server you will be able to filter and return only required items, depending on the valid flag and new/existed state of the item.


    I'm attaching a sample which demonstrate similar technique. Please give it a try for more details on the implementation.


    Nikolay Rusev
    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 Feedback Portal and vote to affect the priority of the items
Back to Top