CF西西的专栏

记录我程序员的旅程

在mvc3下使用infragistics控件,将odata+wcf传回的数据绑定到grid

参考文章:http://community.infragistics.com/blogs/taz_abdeali/archive/2011/05/09/using-netadvantage-jquery-grid-in-asp-net-mvc.aspx

1.添加引用


2.添加


3.在model中构建类 AccountModels

 public class AccountModels
    {
        public static IQueryable<BankAccount> GetAccountList()
        {
            List<BankAccount> accountList = new List<BankAccount>();
            DateTime date = DateTime.Now;
            for (int i = 1; i < 1001; i++)
            {
                accountList.Add(new BankAccount()
                {
                    AccountNumber = i,
                    AccountName = "Test" + i.ToString(),
                    AccountDate = date,
                    AccountType = "chk",
                    AccountBalance = 12345678.90M
                }
                );
            }
            return accountList.AsQueryable<BankAccount>();
        }
  

        public static IQueryable<Product> GetProductList()
        {
            var context = new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc/"));

            var products = from product in context.Products
                           select product;
            //ViewData["ProductList"] = products.ToList();
            return products.ToList().AsQueryable<Product>();
        }

3.构建类BankAccount

  public class BankAccount
    {
        public int AccountNumber { get; set; }
        public string AccountName { get; set; }
        public DateTime AccountDate { get; set; }
        public string AccountType { get; set; }
        public decimal AccountBalance { get; set; }
    }

4.controller定义

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [GridDataSourceAction]
        public ActionResult GetAccountList()
        {
            
            //return View(Models.AccountModels.GetAccountList());
            return View(Models.AccountModels.GetProductList());
        }
    }

5.定义View

@{
    ViewBag.Title = "Home Page";
}
@using Infragistics.Web.Mvc;  
@using MvcApplication5.Models; 
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js" type="text/javascript"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>
<script src="@Url.Content("~/Scripts/IG/ig.ui.min.js")" type="text/javascript"></script>



<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.

</p>
<div>
@( Html.Infragistics().Grid<MvcApplication5.NorthwindService.Product>()
    .ID("igGrid1")
    .Columns(column =>
    {
        column.For(x => x.ProductID).DataType("int").HeaderText("Account Number");
        column.For(x => x.ProductName).DataType("string").HeaderText("Account Name");
        //column.For(x => x.AccountDate).DataType("date").HeaderText("Account Date");
        //column.For(x => x.AccountType).DataType("string").HeaderText("Account Type");
        //column.For(x => x.AccountBalance).DataType("number").HeaderText("Account Balance");
    })
    .Features(features =>
    {
        features.Paging().PageSize(20).PrevPageLabelText("Previous").NextPageLabelText("NEXT");
        features.Sorting().Mode(SortingMode.Single).ColumnSettings(settings =>
        {
            settings.ColumnSetting().ColumnKey("AccountNumber").AllowSorting(true);

        });
        features.Selection().MouseDragSelect(true).MultipleSelection(true).Mode(SelectionMode.Row);
    })
    .ClientDataSourceType(ClientDataSourceType.JSON)
    .DataSourceUrl(Url.Action("GetAccountList"))
    .Width("40%")
    .Height("350")
    .LocalSchemaTransform(true)
    .DataBind()
    .Render()  
)
</div>

显示效果


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoguang44/article/details/6867362
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭