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

Binding to ViewBag in Razor Pages

2 Answers 887 Views
AutoComplete
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 2
John asked on 17 Jan 2018, 03:15 AM

Using ASP.NET Core 2.0 Razor Pages

Currently I have a cshtml page that renders a combo/dropdown

 

<select asp-for="DoctorPractice.DoctorId" class="form-control" asp-items="ViewBag.Doctors"></select>

the PageModel for the view contains some code to populate the ViewBag

 var doctors = _context.Doctors;
ViewData["DoctorId"] = new SelectList(doctors, "Id", "FullName");

 

This all works fine - however, now I wish to use the Telerik AutoComplete control because of it's nice features.

I tried this code below without success - i just get js errors.

 @(Html.Kendo().AutoComplete()
                  .Name("doctors")
                  .Filter("startswith")
                  .Placeholder("Select doctor...")
                  .BindTo(ViewBag.Doctors)
             )

Please can I have an example for Razor Pages using the AutoComplete control?

Thanks,

 

 

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Nencho
Telerik team
answered on 19 Jan 2018, 03:16 PM
Hello John,

I have performed some local tests and it seems like everything is properly working at my end. Since I have used an array of SelectListItems, I have cast the ViewData to IEnumarable as demonstrated below:
cshtml

@(Html.Kendo().AutoComplete()
    .Name("test")
    .Filter("startswith")
    .Placeholder("Select doctor...")
    .DataTextField("Text")
    .BindTo(ViewData["Doctors"] as IEnumerable<SelectListItem>)
)
 
 cshtml.cs
------------------------- 
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
            this.ViewData["Doctors"] = new List<SelectListItem>()
            {
                new SelectListItem() { Value = "1", Text = "First"},
                new SelectListItem() { Value = "2", Text = "Second"},
                new SelectListItem() { Value = "3", Text = "Third"},
                new SelectListItem() { Value = "4", Text = "Forth"}
            };
        }
    }

In addition, you can find attached the sample project that I had tested with.

Please give it a try at your end and let me know the result.



Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
John
Top achievements
Rank 2
answered on 21 Jan 2018, 08:19 AM

Hi Nenco, thanks for the assistance.  I will try this out and I'm sure it'll be all good.

Cheers, John

Tags
AutoComplete
Asked by
John
Top achievements
Rank 2
Answers by
Nencho
Telerik team
John
Top achievements
Rank 2
Share this question
or