Quantcast
Channel: Форум программистов и сисадминов Киберфорум
Viewing all articles
Browse latest Browse all 516754

Вывод пустого grid при использовании dojo для Odata - C# ASP.NET MVC

$
0
0
Здравствуйте!
Сделала проект, основанный на протоколе OData. Сode first для создания бд.
Пытаюсь вывести данные из бд в grid, используя при этом dojo (файл Products.cshtml). Сам grid при этом отрисовался, но данных там не появилось:

Вложение 657010

Подскажите, в чем проблема?

Файл Products.cshtml

Код:

@{
    ViewBag.Title = "Product";
    //Layout = "~/Views/Shared/_Layout.cshtml";
    <script type="text/javascript" src="jquery-1.8.0.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
   
   

}

<link rel="stylesheet"
      href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/resources/dojo.css" />
<link rel="stylesheet"
      href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dijit/themes/claro/claro.css" />
<link rel="stylesheet"
      href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojox/grid/resources/Grid.css" />
<link rel="stylesheet"
      href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojox/grid/resources/claroGrid.css" />

<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js"
        data-dojo-config="async: true, isDebug: true, parseOnLoad: true">
</script>

<script>

    var myStore, dataStore, grid;
   
    require([
            "dojo/store/JsonRest",
            "dojo/store/Memory",
            "dojo/store/Cache",
            "dojox/grid/DataGrid",
            "dojo/data/ObjectStore",
            "dojo/query",
            "dijit/form/Button",
            "dojo/domReady!"
    ], function (JsonRest, Memory, Cache, DataGrid, ObjectStore, query, Button, domReady) {
        //myStore = Cache(JsonRest({
        //    target: "/odata/Category/",
        //    idProperty: "ID"
        //}), Memory({ idProperty: "ID" }));
        myStore = new JsonRest({ target: "/odata/Category" });

        grid = new DataGrid({
            store: dataStore = ObjectStore({ objectStore: myStore }),
            structure: [
                { name: "Номер", field: "ID", width: "50px" },
                { name: "Название", field: "Name", width: "50px" },

            ]
        }, "grid"); // make sure you have a target HTML element with this id

        grid.startup();

        dojo.query("body").addClass("claro");

        grid.canSort = function () { return false };
    });

</script>

<div style="height: 300px; width: 600px; margin: 10px;">
    <div id="grid">
    </div>
</div>

Файл CategoryController

Код:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData;
using ODataSample.Models;

namespace ODataSample.Controllers
{
    public class CategoryController : EntitySetController<Category, int>
    {
        private ODataSampleContext db = new ODataSampleContext();

        public override IQueryable<Category> Get()
        {
            return db.Categories.AsQueryable();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        protected override Category GetEntityByKey(int key)
        {
            return db.Categories.FirstOrDefault(c => c.ID == key);
        }
    }
}

Код:

public class Category
    {
        public int ID { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }

    }
    public class ODataSampleContext : DbContext
    {
        public ODataSampleContext()
            : base("name=ODataSampleContext")
        {
        }
        public DbSet<Category> Categories { get; set; }
        public DbSet <Product>Products { get; set; }
    }

Код:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes
                .FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
                  .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            // Конфигурация и службы Web API
            // Настройка Web API для использования только проверки подлинности посредством маркера-носителя.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            config.EnableQuerySupport();
         
            ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.EntitySet<Category>("Category");
            modelBuilder.EntitySet<Product>("Product");
            modelBuilder.EntitySet<ProductDTO>("ProductDTO");
            Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
            config.Routes.MapODataRoute("ODataRoute", "odata", model);
            // Маршруты Web API
            //config.MapHttpAttributeRoutes();

            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);
        }
    }

P.S. Данные в бд есть:
Вложение 657009

Изображения
Тип файла: png Рисунок2.png (82.0 Кб)
Тип файла: png Рисунок1.png (48.8 Кб)

Viewing all articles
Browse latest Browse all 516754

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>