【问题标题】:JsonRequestBehavior.AllowGet does not exist in current contextJsonRequestBehavior.AllowGet 在当前上下文中不存在
【发布时间】:2019-02-22 05:15:39
【问题描述】:

我正在使用 asp.net MVC 核心,我将在 kendo 下拉列表中从 SQL 读取数据。 我也安装了 Newtonsoft.Json 库。我看到下拉列表,但无法在下拉列表中加载数据。我的代码如下:

我的模型位于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; }
   }
}

我的控制器位于 Controllers>planController.cs :

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using plan_1.Models;
using Newtonsoft.Json;
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);
    }
  }
}

我在views>plan>index.cshtml中的视图如下:

@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

【问题讨论】:

  • 您是否想简单地将列表返回到剑道下拉菜单?

标签: asp.net-core-mvc kendo-asp.net-mvc kendo-dropdown


【解决方案1】:
            .DataTextField("Iata")
            .DataValueField("Id")

是您在下拉列表中定义的 id 和值,但是您从控制器传回 _IdOrigin。我建议更改其中任何一个以匹配,以便它可以绑定到模型。我还建议在您的下拉列表中添加.AutoBind(true)

【讨论】:

  • 感谢您的回答。我做到了,但它不起作用。
猜你喜欢
  • 2023-03-30
  • 2020-03-10
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多