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

Foreign key dropdown depending on content

2 Answers 245 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Leo
Top achievements
Rank 1
Leo asked on 06 Apr 2016, 12:08 PM
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();
        }

@(Html.Kendo().Grid<Data.EF.Models.Product>()
      .Name("grid")
      .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 => {
          toolbar.Create();
          toolbar.Excel();
      })
      .Editable(editable => editable.Mode(GridEditMode.InLine))
      .Sortable(sortable => {
          sortable.SortMode(GridSortMode.MultipleColumn);
      })
      .Filterable()
      .Scrollable(scrollable => scrollable.Enabled(false))
      .DataSource(dataSource => dataSource
          .Ajax()
          .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 Answers, 1 is accepted

Sort by
0
Leo
Top achievements
Rank 1
answered on 07 Apr 2016, 03:01 PM
To rephrase the question: How can you provide a different dataset to the foreignkeycolumn for add / edit / read mode?
0
Accepted
Nikolay Rusev
Telerik team
answered on 08 Apr 2016, 09:02 AM

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.

 

Regards,
Nikolay Rusev
Telerik
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
Tags
Grid
Asked by
Leo
Top achievements
Rank 1
Answers by
Leo
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or