C#更改列项的显示名称

时间:2013-07-11 12:42:55

标签: gridview kendo-ui boolean inline

我有一个kendo gridview,其中一列是布尔值。在列中,信息标记为“true”(对于1或true),“false”(对于0或false)或空白(对于null)。这看起来不太专业,我试图在网格中更改这些值,而不是显示为是,否或无(分别)。我试图实现这一目的的代码是:

型号:

namespace MvcApplication.Models
{
    public class Companies
    {

        public string IsAdmitted { get; set; }

控制器:

    [HttpPost]
    public ActionResult Companies_Read([DataSourceRequest] DataSourceRequest request)
    {
        using (var companydata = new CompaniesDataContext())
        {
            List<Companies> model = new List<Companies>();
            var companylist = companydata.Companies_JBs.Select(s => s).ToList();
            foreach (var h in companylist)
            {
                Companies x = new Companies();
                x.IsAdmitted = h.IsAdmitted == (bool)true ? x.IsAdmitted = "Yes" : h.IsAdmitted == (bool)false ? x.IsAdmitted = "No" : x.IsAdmitted = "Null";
                model.Add(x);
            }

            return Json(companydata.Companies_JBs.ToDataSourceResult(request));
        }
    }

查看:

@(Html.Kendo().Grid(Model)
    .Name("Company_Grid")
    .Columns(columns =>
        {
            columns.Bound(o => o.IsAdmitted).Title("Admitted:").Width(95);
            columns.Command(command => { command.Edit(); command.Destroy(); });
         }
    )
    .ToolBar(toolbar => toolbar.Create().Text("Add a Company"))
    .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Companies"))
    .Pageable()
    .Sortable()
    .Scrollable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(o => o.ID))
        .Create(update => update.Action("Companies_Create", "Home"))
        .Update(update => update.Action("Companies_Update", "Home"))
        .Destroy(update => update.Action("Companies_Destroy", "Home"))
        .Read(read => read.Action("Companies_Read", "Home"))
    )
)

当我放置一个断点并检查该值时,它表示IsAdmitted字段项标记为Yes,No或None(取决于它们在我的sql数据库中的值)。但是,在网格本身中,它们仍然标记为true,false或空白。这是我的问题,还是有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

为什么要归还Json(companydata.Companies_JBs.ToDataSourceResult(request));

您必须返回model。因此,控制器方法的return语句应为

return Json(model.ToDataSourceResult(request));