You can use the RenderValueAs method to render a custom html markup in the grid cell. You must disable the default encoding and satinizing cell values using Encoded and Sanitized methods.
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(30)
.RenderValueAs(o => @<button type="submit">Submit</button>);
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(30)
.RenderValueAs(o => Html.CheckBox("checked", false));
You can render any custom layout using razor @helper:
@helper CustomRenderingOfColumn(Order order)
{
if (order.Customer.IsVip)
{
<text>Yes</text>
}
else
{
<text>No</text>
}
}
@await Html.Grid(Model).Columns(columns =>
{
columns.Add(o => o.Customer.IsVip)
.Titled("Vip customer")
.SetWidth(150)
.RenderValueAs(o => CustomRenderingOfColumn(o));
}).RenderAsync()
You can also use the RenderComponentAs method to render a custom view component in the grid cell:
columns.Add().RenderComponentAs<ButtonCellViewComponent>(returnUrl);
RenderComponentAs method has 2 optional parameters:
Parameter | Type | Description |
---|---|---|
Actions | IList<Action> (optional) | the grid view can pass a list of Actions to be used by the view component (see Passing grid state as parameter) |
Object | object (optional) | the grid view can pass an object to be used by the view component (see Passing grid state as parameter) |
If you use any of these paramenters, you must use them when creating the view component.
You must also create a viewcomponent. The InvokeAsync method includes a mandatory parameter called Item, and 3 optional parameters:
Parameter | Type | Description |
---|---|---|
Item | object (mandatory) | the row item that will be used by the view component |
Grid | IGrid (optional) | Grid can be used to get the grid state (see Passing grid state as parameter) |
Actions | IList<Action> (optional) | the parent component can pass a list of Actions to be used by the view component (see Passing grid state as parameter) |
Object | object (optional) | the parent component can pass an object to be used by the view component (see Passing grid state as parameter) |
Actions and Object must be used when calling the RenderComponentAs method, but Grid can be used without this requirement.
The view component can include any html elements as well as any event handling features.
An example of view component is:
public class ButtonCellViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(object Item, IGrid Grid, object Object)
{
int orderId = ((Order)Item).OrderID;
ViewData["gridState"] = Grid.GetState();
ViewData["returnUrl"] = (string)Object;
var factory = Task<IViewComponentResult>.Factory;
return await factory.StartNew(() => View(orderId));
}
And the view:
@model int
@{
string gridState = (string)ViewData["gridState"];
string returnUrl = (string)ViewData["returnUrl"];
}
<b><a class='modal_link' href='/Home/Edit/@Model?returnUrl=@returnUrl&gridState=@gridState'>Edit</a></b>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。