DBA

Dev Extreme Data Grid Data Binding - @model, Mvc(), WebApi() 본문

[3] Development/MVC(ASP.NET with C#)

Dev Extreme Data Grid Data Binding - @model, Mvc(), WebApi()

코볼 2023. 1. 6. 15:20
728x90
반응형
SMALL

@model

Index.cshtml

----------------------------------------------------------------------------------------------------------------------------

@model List<DevExtremeMvcApp1.Models.PGT_FactSalesQuotaResult>

@(Html.DevExtreme().DataGrid()
        .ID("gridContainerModel")
        .DataSource(Model)
        .KeyExpr("SalesQuotaKey")
        .ShowBorders(true)
        .Columns(c =>
        {
            c.Add().DataField("SalesQuotaKey");
            c.Add().DataField("Title");
            c.Add().DataField("Date");
            c.Add().DataField("SalesAmountQuota");
        })
)

----------------------------------------------------------------------------------------------------------------------------

 

PivotGridController.cs

----------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

 

// using 추가 - Models

using DevExtremeMvcApp1.Models;

 

namespace DevExtremeMvcApp1.Controllers
{
    public class PivotGridController : Controller
    {
        // GET: PivotGrid
        public ActionResult Index()
        {
            List<PGT_FactSalesQuotaResult> result;
            using(PGTDataContext PGT = new PGTDataContext())
            {
                result = PGT.PGT_FactSalesQuota(2011).ToList();
            }
            return View(result);
        }
    }
}

----------------------------------------------------------------------------------------------------------------------------

@model 이용

Mvc() 이용 : 

Index.cshtml

----------------------------------------------------------------------------------------------------------------------------

@using DevExtremeMvcApp1.Models

 

@(Html.DevExtreme().DataGrid<PGT_FactSalesQuotaResult>()
        .ID("gridContainer")
        .DataSource(d => d.Mvc().Controller("PivotGrid").LoadAction("PivotGridList").LoadParams(new { calendarYear = 9999 }))
        .ShowBorders(true)
        .Columns(c =>
        {
            c.AddFor(m => m.SalesQuotaKey).Width(150);
            c.AddFor(m => m.Title).Width(150);
            c.AddFor(m => m.Date).Width(150);
            c.AddFor(m => m.SalesAmountQuota).Width(150);
        })

)

----------------------------------------------------------------------------------------------------------------------------

 

PivotGridController.cs

----------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

 

// using 추가 - Models, Newtonsoft.Json

using DevExtremeMvcApp1.Models;

using Newtonsoft.Json;

 

namespace DevExtremeMvcApp1.Controllers
{
    public class PivotGridController : Controller
    {
        // GET: PivotGrid
        public ActionResult PivotGridList(short? calendarYear)
        {
            if (calendarYear == 9999)
            {
                calendarYear = null;
            }
            List<PGT_FactSalesQuotaResult> result;
            using (PGTDataContext PGT = new PGTDataContext())
            {
                result = PGT.PGT_FactSalesQuota(calendarYear).ToList();
            }
            var resultJson = JsonConvert.SerializeObject(result);
            return Content(resultJson, "application/json");
        }
    }
}

----------------------------------------------------------------------------------------------------------------------------

Mvc() 이용

WebApi() 이용 : 

Index.cshtml

----------------------------------------------------------------------------------------------------------------------------

@using DevExtremeMvcApp1.Models

@(Html.DevExtreme().DataGrid<PGT_FactSalesQuotaResult>()
        .ID("gridContainerApi")
        .DataSource(d => d.WebApi().Controller("PivotGridApi").LoadAction("PivotGridList").LoadParams(new { calendarYear = 9999 }))
        .ShowBorders(true)
        .Columns(c =>
        {
            c.AddFor(m => m.SalesQuotaKey).Width(150);
            c.AddFor(m => m.Title).Width(150);
            c.AddFor(m => m.Date).Width(150);
            c.AddFor(m => m.SalesAmountQuota).Width(150);
        })
)

----------------------------------------------------------------------------------------------------------------------------

 

PivotGridApiController.cs - Api Controller를 추가 해야 함.

----------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

 

// using 추가 - Models, DevExtreme.AspNet.Data, DevExtreme.AspNet.Mvc
using DevExtreme.AspNet.Data;
using DevExtreme.AspNet.Mvc;
using DevExtremeMvcApp1.Models;

 

namespace DevExtremeMvcApp1.Controllers
{
    public class PivotGridApiController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage PivotGridList(short? calendarYear, DataSourceLoadOptions loadOptions)
        {
            if (calendarYear == 9999)
            {
                calendarYear = null;
            }
            List<PGT_FactSalesQuotaResult> result;
            using (PGTDataContext PGT = new PGTDataContext())
            {
                result = PGT.PGT_FactSalesQuota(calendarYear).ToList();
            }
            return Request.CreateResponse(DataSourceLoader.Load(result, loadOptions));
        }
    }
}

----------------------------------------------------------------------------------------------------------------------------

WebApi() 이용

 

 

 

 

728x90
반응형
LIST
Comments