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

How 2 set Foreign key column use Tag helper?

12 Answers 428 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vincent
Top achievements
Rank 1
Iron
Vincent asked on 14 Mar 2019, 06:16 AM
How 2 set Foreign key column use Tag helper? Please help.

12 Answers, 1 is accepted

Sort by
1
Georgi
Telerik team
answered on 18 Mar 2019, 02:38 PM
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.
0
mina
Top achievements
Rank 1
answered on 05 May 2020, 12:00 PM

hi what about now ? 

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

0
Georgi
Telerik team
answered on 08 May 2020, 10:41 AM

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.
0
Tim
Top achievements
Rank 1
answered on 08 Jun 2020, 04:51 PM

Hi Georgi,

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

Thanks

0
Georgi
Telerik team
answered on 11 Jun 2020, 12:29 PM

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.
0
Scott
Top achievements
Rank 1
answered on 23 Jun 2020, 06:20 PM
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?
0
Georgi
Telerik team
answered on 26 Jun 2020, 11:43 AM

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.
0
Wolfgang Weilguny
Top achievements
Rank 1
Veteran
answered on 06 Aug 2020, 09:36 AM

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)?

0
Georgi
Telerik team
answered on 11 Aug 2020, 08:00 AM

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

0
Wolfgang Weilguny
Top achievements
Rank 1
Veteran
answered on 12 Aug 2020, 09:54 AM

Hey Georgi,

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

Best Regards

0
Frank
Top achievements
Rank 1
answered on 29 Sep 2020, 08:53 AM

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?

0
Eyup
Telerik team
answered on 02 Oct 2020, 07:10 AM

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/.

Tags
Grid
Asked by
Vincent
Top achievements
Rank 1
Iron
Answers by
Georgi
Telerik team
mina
Top achievements
Rank 1
Tim
Top achievements
Rank 1
Scott
Top achievements
Rank 1
Wolfgang Weilguny
Top achievements
Rank 1
Veteran
Frank
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or