您的位置:首页 > 编程语言 > ASP

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

2011-10-13 09:28 351 查看
参考文章: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>


显示效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mvc wcf asp.net string date class