How to specify editor which is in partial view for the form's field

1 Answer 136 Views
Form Grid
kva
Top achievements
Rank 2
Iron
Iron
Iron
kva asked on 14 Aug 2023, 05:36 AM

I have a dropdown editor in the partial view:

@(Html.Kendo().DropDownListFor(m => m.DepartmentId)
    .DataTextField("Name")
    .DataValueField("Id")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("GetDepartments", "Home");
        });
    })
    )

Which I use in grid by specifying partial view's file name:

 .EditorTemplateName("Department")
How to specify the same for the form's field?

1 Answer, 1 is accepted

Sort by
1
Accepted
Mihaela
Telerik team
answered on 15 Aug 2023, 04:07 PM

Hello,

To load the DropDownList editor through a Partial View, you can use the EditorTemplateView() method of the Form item:

//~Views/Home/Index.cshtml

@(Html.Kendo().Form<UserViewModel>()
        .Name("formExample")
        .HtmlAttributes(new { action = "Index", method = "POST" })
        .Items(items =>
        {
            items.Add()
                .Field(f => f.Id)
                .Label(l => l.Text("Id:"))
                .EditorTemplateView(Html.Partial("_DropDownTemplatePartial"));
        })
    )

//~Views/Home/_DropDownTemplatePartial.cshtml
@{
    Layout = null;
}

@(Html.Kendo().DropDownList()
     .Name("Id") //Make sure that the Name() of the DropDown matches the name of the Model property
    .DataTextField("Name")
    .DataValueField("Id")
    .DataSource(...)
)

Regards, Mihaela Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
kva
Top achievements
Rank 2
Iron
Iron
Iron
commented on 16 Aug 2023, 04:56 AM

I don't have method EditorTemplateView. I'm using version 2022.1.
Mihaela
Telerik team
commented on 16 Aug 2023, 02:54 PM

The EditorTemplateView() option was introduced in version 2023.1.314.

If you use an older version, you can use:

  • the EditorTemplateId() option to load the Partial View through Kendo UI Template:
                i.Add()
                    .Field(f => f.Id)
                    .Label(l => l.Text("Id:"))
                    .EditorTemplateId("customEditor");

<script id="customEditor" type="text/x-kendo-template">
    @Html.Partial("_DropDownTemplatePartial")
</script>
  • the Editor() configuration to enable the built-in DropDownList editor of the Form:
                i.Add()
                    .Field(f => f.Id)
                    .Label(l => l.Text("Id:"))
                    .Editor(e =>
                    {
                        e.DropDownList()
                        .DataTextField("Name")
                        .DataValueField("Id")
                        .DataSource(...);
                    });

 

Tags
Form Grid
Asked by
kva
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Mihaela
Telerik team
Share this question
or