【问题标题】:ASP.NET MVC kendo gridASP.NET MVC 剑道网格
【发布时间】:2014-10-10 15:57:12
【问题描述】:

只有当我直接传递模型时,我才能填充网格中的项目。如果我尝试获取 Json 数据,我不能。

这是我的控制器:

namespace MVC_Test1.Controllers
{
    public class StudentsController : Controller
    {
        private ContosoUniversityEntities db = new ContosoUniversityEntities();

        public ActionResult Index()
        {
            db.Configuration.ProxyCreationEnabled = false;
            return View(db.students.ToList());
        }


        public ActionResult Read([DataSourceRequest] DataSourceRequest request)
        {
            using (var northwind = new ContosoUniversityEntities())
            {
                IQueryable<student> students = northwind.students;
                DataSourceResult result = students.ToDataSourceResult(request);

                return Json(result);
            }
        }
    }
}

如果我有这个视图,网格可以工作并显示所有项目:

@model MVC_Test1.Models.student

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@*@(Html.Kendo().Grid(Model)
    .Name("grid")
    .DataSource(dataSource => dataSource
        .Ajax()
        .ServerOperation(false)
    )
    .Columns(columns =>
    {
        columns.Bound(p => p.FirstName);
        columns.Bound(p => p.LastName);
    })
)*@

但是如果我尝试从 json 读取它就不起作用。它显示一个空网格。

@(Html.Kendo().Grid<MVC_Test1.Models.student>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.FirstName);
        columns.Bound(p => p.LastName);
    })
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Read", "Students"))
    )
)

我确定我做错了什么,但我想不通。感谢您的帮助。

【问题讨论】:

  • 这可能是因为您使用的是IQueryable 而不是List
  • 浏览器控制台是否出现错误?您的请求是否已付诸行动?
  • 我已更改为使用列表,但仍然没有数据。没有错误,是的,它正在执行操作,我看到它正在检索数据,但列表仍然是空的......这很奇怪

标签: jquery asp.net-mvc json kendo-grid


【解决方案1】:

最后,在尝试了一切之后(感谢大家),我开始了一个新的项目,并且成功了。我不知道出了什么问题,但现在它正在工作。谢谢

【讨论】:

    【解决方案2】:

    我也看不出有那么多问题。但是尝试三件事,允许获取。

    return Json(result, JsonRequestBehavior.AllowGet);
    

    其次,我认为这不是问题,但请尝试使用 List 而不是 IQueryable。

    最后,尝试返回 JsonResult 而不是 ActionResult

    希望这会有所帮助。

    【讨论】:

    • 它打到后端了吗?我的意思是学生控制器中的 Read() 函数。它确实返回值,对吗??
    • JsonResult 一个 ActionResult (它是一个派生类型),但使用更具体的返回类型并没有真正改变任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多