Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using asp.net MVC core and I am going to read data from SQL in kendo drop-down list. I have installed Newtonsoft.Json library too. I see drop-down list but I can't load data in my drop-down list. my code is as below:

What I have tried:

<pre>my model is located in models>Airports.cs:


using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace plan_1.Models
{
public class Airport: BaseEntity  
   { 
    public int Id { get; set; }
    public string Iata { get; set; }
    public string Icao { get; set; }
    public string LogoUrl { get; set; }
    public int IsBased { get; set; }
    public int CityId { get; set; }
    public virtual City City { get; set; }

    public Airport()
    {
        this.Terminals = new 
          HashSet<Terminal>();
    }
     public ICollection<Terminal> Terminals 
     { get; set; }
   }
}


My controller is located in Controllers>planController.cs :


using System.Linq;
using Microsoft.AspNetCore.Mvc;
using plan_1.Models;
namespace plan_1.Controllers
{
 public class planController : Controller
 {        
    public ActionResult Index()
    {
        return View();
    }
    public JsonResult GetAirPort()
    {
      plan_1Context dbContext = new 
      plan_1Context();

        return 
        Json(dbContext.AirPorts.Select(O => 
        new { _Id = O.Id, Origin = O.Iata 
        }), 
     JsonRequestBehavior.AllowGet);
    }
  }
}


and my view located in views>plan>index.cshtml is as below:


@model IEnumerable<plan_1.Models.Airport>
@{
 ViewData["Title"] = "Planing";
 }

 <div>
    <h4>Origin:</h4>
    @(Html.Kendo().DropDownList()
                .Name("Origin")
                .HtmlAttributes(new { style 
    = "width:100%" })
                .OptionLabel("Select 
     category...")
                .DataTextField("Iata")
                .DataValueField("Id")
                .DataSource(source =>
                {
                    source.Read(read =>
                    {                  
read.Action("GetAirPorts", 
       "planController");
                    });
                })
    )
</div>
also, I should mix the airplane model by 
the plan model, I think I should use the 
view model to mix them.

Please help what should I do? It is days 
that I am looking for the answer
Posted
Updated 21-Feb-19 22:22pm

1 solution

It's fine not to use JsonRequestBehavior.AllowGet anymore.
Just use
C#
return Json(dbContext.AirPorts
	.Select(O => new { _Id = O.Id, Origin = O.Iata}));

instead.
And yes it's fine to mix models inside view models.
 
Share this answer
 
Comments
afagh 22-Feb-19 8:05am    
hi Bohdan, I did it before but my page goes empty and the other items are not showing
Bohdan Stupak 22-Feb-19 8:26am    
But backend returns items correctly, right? It is client who mishandles them?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900