How to bind dropdownlist in MVC grid, but from underlying model

0 Answers 112 Views
DropDownList Grid
FranckSix
Top achievements
Rank 2
Iron
Iron
Veteran
FranckSix asked on 13 Jun 2023, 02:01 PM

Hi,

From this Model : 

publicclassMyRowData
{
   public int Id { get; set; }
   public SelectList Values { get; set; }
   public string Value { get; set; }
}

publicclassMyGridData
{
   public IEnumerable<MyRowData> Rows { get; set; }
}

I want to bind my model to a grid in view like :

 @(Html.Kendo.Grid(Model.Rows)
	.Name("myGrid")
	.Columns(c =>
	{
		c.Bound(i => i.Id).Visible(false);
		c.ForeignKey(t => t.Value, t => t.Values); //Here is the bind for drop downlist (différent for each row
                                                           //And how can achieve this ?
	})
	.Navigatable()
	.Scrollable()))

Thanks for help

Anton Mironov
Telerik team
commented on 16 Jun 2023, 06:02 AM

Hi FranckSix,

Thank you for the code snippets and details provided.

In order to use a Foreign Key column, I would recommend using the approach from the following demo:

The following article describes how a Foreign Key column should be added for a Telerik UI Grid:

I hope this information helps.

Kind Regards,
Anton Mironov

FranckSix
Top achievements
Rank 2
Iron
Iron
Veteran
commented on 19 Jun 2023, 01:12 PM

Hi Anton,

Thank you for response.

If I understand there is no aletrnative than passing by controller to get values.
Because I have already values in my model but not on the main Model the values is on the Elements list.

I hoped to get an alternative to Controller method. Like a JQuery method to get values from model. Or a Template to create manually the DropDownList.
Anton Mironov
Telerik team
commented on 22 Jun 2023, 07:34 AM

Hi FranckSix,

Thank you for the additional details provided.

Yes, you can use a template. These templates are called EditorTemplates.

We have default EditorTemplates files placed in the "~Views/Shared/EditorTemplates" folder of every Telerik project.

Feel free to add your custom file there.

In order to point to a field which file to use, just add a UI Hint annotation in the Model. Here is an example:

public class Order
{
    public int OrderID { get; set; }

    public string ShipCountry { get; set; }

    [UIHint("EmployeeEditor")]
    public Employee Employee { get; set; }
}
The Employee field above will use the "~Views/Shared/EditorTemplates/EmployeeEditor.cshtml" as an editor in a Grid which uses "Order" as a Model.

The following article describes the use of the EditorTemplates in more detail:

I hope this information helps. Let me know if further information or assistance is needed.


Best Regards,
Anton Mironov

 

No answers yet. Maybe you can help?

Tags
DropDownList Grid
Asked by
FranckSix
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or