How 2 set Foreign key column use Tag helper?

13 posts, 0 answers
  1. Vincent
    Vincent avatar
    56 posts
    Member since:
    Dec 2013

    Posted 14 Mar 2019 Link to this post

    How 2 set Foreign key column use Tag helper? Please help.
  2. Georgi
    Admin
    Georgi avatar
    698 posts

    Posted 18 Mar 2019 Link to this post

    Hi Vincent,

    Currently the Grid TagHelper does not contain a foreign key column configuration. However, I am glad to confirm that we are already working on implementing a foreign key column for the tag helper of the grid.

    Currently as a workaround I can suggest you to use column templates and manually map the key value pairs.


    Regards,
    Georgi
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. mina
    mina avatar
    1 posts
    Member since:
    Dec 2019

    Posted 05 May 2020 in reply to Georgi Link to this post

    hi what about now ? 

    when will kendo grid have foreign key when we use tag helper in our mvc core projects ? 

  4. Georgi
    Admin
    Georgi avatar
    698 posts

    Posted 08 May 2020 Link to this post

    Hello Mina,

    Foreign key column is now available for the tag helper of the grid and can be configured as follows:

    <kendo-grid name="grid" height="550">
    ...
            <foreign-key-column field="fieldName" values="..." value-field="ValueField" text-field="TextField"></foreign-key-column>

    Regards,
    Georgi
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  5. Tim
    Tim avatar
    20 posts
    Member since:
    Oct 2015

    Posted 08 Jun 2020 in reply to Georgi Link to this post

    Hi Georgi,

    Can you provide more information and an example of using the foreign-key-column?

    Thanks

  6. Georgi
    Admin
    Georgi avatar
    698 posts

    Posted 11 Jun 2020 Link to this post

    Hi Tim,

    The tag helper configuration is equivalent to the html helper foreign key column configuration. You need to specify the value and text fields and the source of values.

    e.g.

    // in controller store the data for example in the ViewData
    
            public ActionResult ForeignKeyColumn()
            {            
                PopulateCategories();
                return View();
            }
    
            private void PopulateCategories()
            {
                using (var dataContext = new SampleEntitiesDataContext())
                {
                    var categories = dataContext.Categories
                                .Select(c => new CategoryViewModel
                                {
                                    CategoryID = c.CategoryID,
                                    CategoryName = c.CategoryName
                                })
                                .OrderBy(e => e.CategoryName);
    
                    ViewData["categories"] = categories.ToList();
                    ViewData["defaultCategory"] = categories.First();
                }
            }
    
    // configure foreignkey column
     
    <kendo-grid name="grid" height="550">
    ...
            <foreign-key-column field="Category" values=" (System.Collections.IEnumerable)ViewData["categories"]" value-field="CategoryID" text-field="CategoryName"></foreign-key-column>
    
    

    Regards,
    Georgi
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  7. Scott
    Scott avatar
    2 posts
    Member since:
    Mar 2020

    Posted 23 Jun 2020 in reply to Georgi Link to this post

    The foreign key tag isn't working for me. It's complaining about ViewData saying "there is no argument given that corresponds to the required formal parameter 'index' of ...". Is there something missing from your code?
  8. Georgi
    Admin
    Georgi avatar
    698 posts

    Posted 26 Jun 2020 Link to this post

    Hello Scott,

    Please note that the ViewData value is cast to an IEnumerable. Did you cast the ViewData in your grid? Could share your configuration with us?

    Regards,
    Georgi
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  9. Wolfgang Weilguny
    Wolfgang Weilguny avatar
    12 posts
    Member since:
    Feb 2010

    Posted 06 Aug 2020 Link to this post

    Hey Georgi,

    I am interested in using the ForeignKeyColumn Tag-Helper, however I have found that it does not work in my project while the ForeignKeyColumn HTML-Helper does work. I attached the two approaches, they are almost indentical but the ForeignKeyColumns behave differently:

    @(Html.Kendo().Grid<Data.CarModel>()
        .Name("modelGridWorking")
        .Columns(c => {
            c.ForeignKey("BrandId", Model.Brands, "BrandId", "BrandName").Title("Marke").Width(100);
            c.Bound("ModelName").Title("Modell").Width(100);
            c.Bound("Description").Title("Beschreibung").Width(200);
            c.Bound("Locked").Title("Gesperrt").Width(50);
            c.Command(cmd => cmd.Edit()).Width(120);
            c.Command(cmd => cmd.Destroy()).Width(120);
        })
        .DataSource(c => {
            c.Ajax()
            .ServerOperation(true)
            .Read(o => o.Url("ModelManagement?handler=Read").Data("forgeryToken"))
            .Create(o => o.Url("ModelManagement?handler=Create").Data("forgeryToken"))
            .Update(o => o.Url("ModelManagement?handler=Update").Data("forgeryToken"))
            .Destroy(o => o.Url("ModelManagement?handler=Destroy").Data("forgeryToken"))
            .Model(m => {
                m.Id("ModelId");
                m.Field("ModelName", typeof(String));
                m.Field("Description", typeof(String));
                m.Field("Locked", typeof(Boolean));
            });
        })
        .Editable(GridEditMode.InLine)
        .Pageable(c => {
            c.Refresh(true).ButtonCount(5).PageSizes(new[] { 10, 20, 50 });
        })
        .Groupable(true)
        .Sortable(true)
    )
    <kendo-grid name="modelGridNotWorking">
            <columns>
                <foreign-key-column title="Marke" width="100" field="BrandId" values="Model.Brands" value-field="BrandId" text-field="BrandName"></foreign-key-column>
                <column title="Modell" width="100" field="ModelName"></column>
                <column title="Beschreibung" width="200" field="Description"></column>
                <column title="Gesperrt" width="50" field="Locked"></column>
                <column width="120">
                    <commands>
                        <column-command text="Bearbeiten" name="edit"></column-command>
                    </commands>
                </column>
                <column width="120">
                    <commands>
                        <column-command text="Löschen" name="destroy"></column-command>
                    </commands>
                </column>
            </columns>
            <datasource type="DataSourceTagHelperType.Ajax" page-size="20">
                <transport>
                    <read url="ModelManagement?handler=Read" data="forgeryToken" />
                    <create url="ModelManagement?handler=Create" data="forgeryToken" />
                    <update url="ModelManagement?handler=Update" data="forgeryToken" />
                    <destroy url="ModelManagement?handler=Destroy" data="forgeryToken" />
                </transport>
                <schema>
                    <model id="ModelId">
                        <fields>
                            <field name="ModelName" type="String"></field>
                            <field name="ModelDescription" type="String"></field>
                            <field name="Locked" type="Boolean"></field>
                        </fields>
                    </model>
                </schema>
            </datasource>
            <editable mode="inline" enabled="true" />
            <pageable button-count="5" refresh="true" page-sizes="new int[] { 10, 20, 50 }"></pageable>
            <groupable enabled="true" />
            <sortable enabled="true" />
        </kendo-grid>

     

    In the Model, i am populating the public Property Brands as follows:

    public IEnumerable<CarBrand> Brands { get; set; }
    public IActionResult OnGet()
    {
        Brands = _context.CarBrand.ToList();
    }

     

    Note the I also tried to use the ViewData instead of a property but the result is the same. Do you have any idea why the ForeignKeyColumn in the first example works as expected (displays the Brand name instead of BrandId) but the later does not (displays BrandId, also when editing it shows an Integer-Editor instead of a DropDown)?

  10. Georgi
    Admin
    Georgi avatar
    698 posts

    Posted 11 Aug 2020 Link to this post

    Hello Wolfgang,

    Your configuration seems correct and I don't see anything obvious which might cause the issue. Could you please check if there are any js errors in the console of your browser when you use the tag helper?

    Having said that, sharing a sample which demonstrates the issue would be of great help to further investigate the case. If a sample is not an option, please send us the serialized initialization script of the grid (from page source, right click the page and click View page source, then find the script of the grid and paste it here).

    Regards,
    Georgi
    Progress Telerik

  11. Wolfgang Weilguny
    Wolfgang Weilguny avatar
    12 posts
    Member since:
    Feb 2010

    Posted 12 Aug 2020 in reply to Georgi Link to this post

    Hey Georgi,

    thanks for your reply, we have worked around the issue by simply sticking to the HTML-helpers.

    Best Regards

  12. Frank
    Frank avatar
    2 posts
    Member since:
    Jan 2017

    Posted 29 Sep 2020 in reply to Georgi Link to this post

    Hello Georgi,

    I can confirm that the foreign-key-column tag (v. 2020.2.617) does not work correctly. Even when the values, value-field and text-field attributes are provided correctly, the text value is not rendered nor is there a dropdown in edit mode.

    Can you check if there is the bug in that version?

  13. Eyup
    Admin
    Eyup avatar
    4074 posts

    Posted 02 Oct 2020 Link to this post

    Hello Frank,

     

    Generally, the basic scenario works in every version. You can check it live here and use it as a reference:
    https://demos.telerik.com/aspnet-core/grid/foreignkeycolumn

    If the issue in your original project remains, I would suggest that you open a formal Support Ticket and send us a very basic runnable isolated version of your application so we can further investigate the issue.

     

    Regards,
    Eyup
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top