【问题标题】:ASP.NET Core 2.0 with Telerik Kendo Grid Read method ([DataSourceRequest]) is not called in publish发布时未调用具有 Telerik Kendo Grid Read 方法 ([DataSourceRequest]) 的 ASP.NET Core 2.0
【发布时间】:2018-12-27 22:32:16
【问题描述】:

我用 Telerik Kendo UI 和 Asp.Net Core 2.0 控件创建了一个应用程序。在本地,我们能够在 Visual Studio 2017 中无错误地运行相同的代码,但在本地 IIS 中发布后,它会出现以下错误(见附图)。

错误:-“http://localhost:91/Masters/GetStateList404(未找到)”。

检查错误时发现只有一个网格的Read方法(可能是'[DataSourceRequest]DataSourceRequest'参数)没有被调用(其他 动作方法被完美地调用,就像下面的代码'GetRecordStatusList()')

控制器:

public class MastersController : Controller
{
    private IAllRepository<StateMaster> iAllStateRepository;

    public IActionResult StateMaster()
    {
        List<SelectListItem> statusList = new List<SelectListItem>() {
            new SelectListItem{Text = "Active", Value = "1" },
            new SelectListItem{Text = "Inactive", Value = "2" }
        };

        HttpContext.Session.SetInt32("UserId", 1);
        HttpContext.Session.SetString("UserName", "Admin");
        ViewBag.UserName = HttpContext.Session.GetString("UserName");

        return View();
    }

    //This action method is not called in published-code
    public ActionResult GetStateList([DataSourceRequest]DataSourceRequest request)
    {
        this.iAllStateRepository = new StateMasterRepository();
        var result = iAllStateRepository.GetModelList();
        var dsResult = result.ToDataSourceResult(request);
        return Json(dsResult);
    }

    public JsonResult GetRecordStatusList()
    {
        List<SelectListItem> statusList = new List<SelectListItem>() {
            new SelectListItem{Text = "Active", Value = "1" },
            new SelectListItem{Text = "Inactive", Value = "2" }
        };
        return Json(statusList);
    }
}

更新:这是视图(StateMaster.cshtml)代码

<div class="row">
     @(Html.Kendo().Grid<Entity.MasterEntity.StateMaster>()
      .Name("StateGrid")
      .Columns(columns =>
      {
       columns.Bound(p => p.StateName).Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(true).Operator("contains"))).Width(120);
       columns.Bound(p => p.Abbr).Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(true).Operator("contains"))).Width(120).MinScreenWidth(800);
       columns.Command(command => { command.Edit(); command.Destroy(); }).Width(70);
      })
      .ToolBar(toolbar =>
      {
       toolbar.ClientTemplateId("toolbarStatus");
      })
      .NoRecords(e => e.Template("<div class='alert alert-warning' style='padding:3px'><h6 class='bold'><i>No data found!</i></h6></div>"))
      .Pageable(p => { p.Refresh(true); p.PageSizes(true); }).Navigatable()
      .Sortable(s => s.SortMode(GridSortMode.MultipleColumn)).Scrollable(s => s.Enabled(true))

      .HtmlAttributes(new { style = "height:100%;" })
      .DataSource(dataSource => dataSource
       .Ajax()
       .PageSize(10)
       .ServerOperation(true)
       .Model(m =>
       {
        m.Id(s => s.StateId);
        m.Field(f => f.StateName);
        m.Field(f => f.Abbr);
        m.Field(f => f.RecordStatus);
       })

       .Read(read => read.Action("GetStateList", "Masters"))

      )
      .Resizable(resize => resize.Columns(false))
     )
</div>

下图为开发中,显示网格的读取方法工作正常。

【问题讨论】:

  • 抱歉这个问题可能很愚蠢,但您是从本地主机访问该网站,而不是从其他网站访问,对吗?您是否将 IIS 中的 URL (localhost) 映射到正确的目录?您是否尝试使用其他网址?
  • 我已正确映射它。在已发布的代码视图中呈现并且“GetRecordStatusList”操作方法正在工作,但与 Kendo Grid Read 方法(“GetStateList”)绑定的操作方法不起作用。
  • @TarakPrajapati 显示发出请求的客户端代码?
  • @Nkosi,我已经用请求的代码更新了问题。还更新了图片。
  • 尝试使用“发布前删除所有现有文件”选项进行发布

标签: c# asp.net-mvc telerik kendo-grid asp.net-core-2.0


【解决方案1】:

我遇到过这样一种情况,如果在站点中其他任何地方的路由中映射区域,它有时会找不到控制器动作。要解决此问题,您可以通过执行以下操作来清除该区域:

  .DataSource(s => s.Ajax().Read(read => read.Action("GetStateList", "Masters", new {area = ""})))

我还建议将[HttpGet] 属性添加到您的操作中。并且你可以通过指定.Type(HttpVerbs.Get) 来强制 Kendo 使用它,如下所示:

  .DataSource(s => s.Ajax().Read(read => read.Action("GetStateList", "Masters", new {area = ""}).Type(HttpVerbs.Get)))

【讨论】:

    【解决方案2】:

    我可以通过在方法上方添加 [AcceptVerbs(HttpVerbs.Post)] 将方法更改为类型 Post 来调用 Action 方法。

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GetStateList([DataSourceRequest]DataSourceRequest request)
        {
            this.iAllStateRepository = new StateMasterRepository();
            var result = iAllStateRepository.GetModelList();
            var dsResult = result.ToDataSourceResult(request);
            return Json(dsResult);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      相关资源
      最近更新 更多