KendoGrid中的外键列不显示任何内容

时间:2016-08-05 03:13:58

标签: asp.net-mvc-4 kendo-grid kendo-asp.net-mvc razor-2

我想在编辑此kendogrid时进行下拉菜单。我使用内联编辑。在我的情况下,当我显示这个kendogrid时,外键列没有显示任何内容。但是当我编辑它时,它会显示数据。

模型

public IEnumerable<KotaPreviewModel> Read()
        {
            return db.Tbl_Kota.Where(kt=>kt.flag=="Y").Select(kota => new KotaPreviewModel
            {
                 id_kota = kota.id_kota,
                 namaKota = kota.nama_kota,
                 id_propinsi = kota.id_propinsi.Value
            });
        }

控制器

public ActionResult GetKtByJSON([DataSourceRequest] DataSourceRequest request)
        {
            return Json(km.Read().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
public ActionResult Kota(int id)
        {
            ViewData["id"] = id;
            ViewData["id_propinsi"] = (from a in db.Tbl_Propinsi
                                        where a.flag == "Y"
                                        select new
                                        {
                                            id_propinsi = a.id_propinsi,
                                            propinsi = a.propinsi
                                        }).ToList();
            return View();
        }

查看

@model IEnumerable<admission.Models.KotaModel>
@{
    ViewBag.Title = "Kota";
    Layout = "~/Views/Shared/_adminLayout.cshtml";
}

<div class="tables">
    <div class="table-responsive bs-example widget-shadow">
        <h4>Data Kota:</h4>
    @(Html.Kendo().Grid<admission.Models.KotaPreviewModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.id_kota).Width(70).Title("No").Hidden();
        columns.Bound(p => p.namaKota).Title("Nama Kota");
        columns.ForeignKey(p => p.id_propinsi, (System.Collections.IEnumerable)ViewData["id_propinsi"], "id_propinsi", "propinsi")
            .Title("Provinsi");
        columns.Command(command => { command.Edit(); command.Destroy().Text("Inactive"); }).Width(250);
    })
    .ToolBar(toolbar => toolbar.Create())
            .Editable(editable => editable.Mode(GridEditMode.InLine))
            .Pageable()
            .Sortable()
            .Filterable()
            .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(5)
        .Events(events => events.Error("error_handler").Sync("sync_handler"))
        .Model(model => model.Id(p => p.id_kota))
        .Create(update => update.Action("Kt_Create", "Lokasi"))
        .Read(read => read.Action("GetKtByJSON", "Lokasi"))
        .Update(update => update.Action("Kt_Update", "Lokasi"))
        .Destroy(update => update.Action("Kt_Delete", "Lokasi"))
    )
)

    </div>
    </div>
<script>
</script>
<script type="text/javascript">
    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
    function sync_handler(e) {
        this.read();
    }
</script>

enter image description here enter image description here

请帮帮我。提前致谢

0 个答案:

没有答案