简体   繁体   中英

ASP.NET Core 2.0 with Telerik Kendo Grid Read method ([DataSourceRequest]) is not called in publish

I have created an application with Telerik Kendo UI and Asp.Net Core 2.0 controls. Locally we are able to run the same code without error in Visual Studio 2017, but after publishing in local IIS it gives below error(see attached image).

Error: - " http://localhost:91/Masters/GetStateList 404 (Not Found)".

在此处输入图片说明

While checking the error found that only Read method (may be due to '[DataSourceRequest]DataSourceRequest' parameter) of a grid is not called (other action method is perfectly called like in below code 'GetRecordStatusList()')

Controller:

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);
    }
}

Updated : This is View (StateMaster.cshtml) code

<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>

Below image is of development, which shows that the grid's read method is working properly.

开发环境中的工作图像

I have run into a situation if areas were mapped in the routing anywhere else in the site, it would sometimes not find come controller actions. To get around this you can clear the area by doing the following:

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

I also recommend adding the [HttpGet] attribute to your action. and you can force Kendo to use it by specifying the .Type(HttpVerbs.Get) on the read like below:

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

I was able to call the Action method by changing the method to type Post by adding the [AcceptVerbs(HttpVerbs.Post)] just above the method.

[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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM