本帖最后由 isnb 于 2018-12-10 10:59 编辑
修改Startup.cs
[C#] 纯文本查看 复制代码 services.AddMvc(); //使用DevExpress 服务
services.AddDevExpressControls();
//使用Sqlite 数据库
services.AddDbContext<DevExpressStarterProject.Models.NorthwindContext>(options => {
options.UseSqlite(Configuration.GetConnectionString("NorthwindConnection"));
使用AJAX控件
在SampleController定义四个回调方法
[C#] 纯文本查看 复制代码 public IActionResult GridViewPartial() {
return PartialView(NorthwindContext.Products);
}
public IActionResult AddNewRow(Product product) {
try {
if(ModelState.IsValid) {
product.ProductID = NorthwindContext.Products.Max(p => p.ProductID) + 1;
NorthwindContext.Products.Add(product);
NorthwindContext.SaveChanges();
}
} catch(Exception e) {
ViewData["error"] = e.Message;
}
return PartialView("GridViewPartial", NorthwindContext.Products);
}
public IActionResult UpdateRow(Product product) {
try {
if(ModelState.IsValid) {
NorthwindContext.Products.Update(product);
NorthwindContext.SaveChanges();
}
} catch(Exception e) {
ViewData["error"] = e.Message;
}
return PartialView("GridViewPartial", NorthwindContext.Products);
}
public IActionResult DeleteRow(int productID = -1) {
var product = NorthwindContext.Products
.FirstOrDefault(i => i.ProductID == productID);
try {
NorthwindContext.Products.Remove(product);
NorthwindContext.SaveChanges();
} catch(Exception e) {
ViewData["error"] = e.Message;
}
return PartialView("GridViewPartial", NorthwindContext.Products);
}
将控件的Razor代码添加到创建的局部视图中。为新操作方法指定AJAX请求路由映射:
[C#] 纯文本查看 复制代码 @(Html.DevExpress()
.BootstrapGridView<Product>("GridView")
.KeyFieldName(m => m.ProductID)
.Columns(columns => {
columns.AddCommandColumn()
.ShowEditButton(true)
.ShowNewButtonInHeader(true)
.ShowDeleteButton(true);
columns.Add(m => m.ProductName);
columns.Add(m => m.QuantityPerUnit);
columns.Add(m => m.UnitPrice);
columns.Add(m => m.UnitsInStock);
columns
.AddTextColumn()
.FieldName("TotalPrice")
.ReadOnly(true)
.UnboundType(DevExpress.Data.UnboundColumnType.Decimal)
.UnboundExpression("UnitPrice * UnitsInStock")
.PropertiesTextEdit(properties => properties.DisplayFormatString("c"))
.SettingsEditForm(editForm => editForm.Visible(false));
columns.Add(m => m.Discontinued);
})
.Settings(settings => settings.ShowHeaderFilterButton(true))
.EditErrorText(ViewData["Error"]?.ToString())
.Routes(routes => routes
.MapRoute(r => r
.Action("GridViewPartial")
.Controller("Sample"))
.MapRoute(r => r
.RouteType(GridViewRouteType.UpdateRow)
.Action("UpdateRow")
.Controller("Sample"))
.MapRoute(r => r
.RouteType(GridViewRouteType.AddRow)
.Action("AddNewRow")
.Controller("Sample"))
.MapRoute(r => r
.RouteType(GridViewRouteType.DeleteRow)
.Action("DeleteRow")
.Controller("Sample")))
.Bind(Model))
自定义控件的外观
套件中的每个控件都有一个CssClasses方法,通过设置该方法改变控件外观
先添加样式代码
| .my-button { border-radius: 0;}
| 生成一个按钮控件并指定样式。
| @(Html.DevExpress() .BootstrapButton("myButton") .CssClasses(css => css.Control("my-button")) .Text("My Button")
|
项目工程仅回复可见。项目依赖以下组件,自行安装。- ASP.Net Core 2.0
- Bootstrap 3.3+ (including Bootstrap 4 Beta)
- JQuery 2+
|