Telerik MVC网格,无法在Ajax编辑上制作下拉列表

时间:2012-01-21 21:03:12

标签: asp.net-mvc-3 telerik-grid telerik-mvc

我正在尝试使用Ajax Editing实现Telerik MVC网格。基本上,它是一类群组,每个群组都属于一个组织,我需要在编辑模式下显示一个下拉列表,其中包含可供选择的组织。

我已经按照教程和本论坛进行了操作,但我无法使其发挥作用。

这是我的代码:

型号:

Partial Public Class hdmtGROUP

    <ScaffoldColumn(False)>
    Public Property gID As Integer

    Public Property gORG As Integer

    'Dropdownlist.
    <UIHint("_OrgDropDownListPartial"), Required()>
    Public Property Organisation As String

    <DisplayName("Name")>
    <Required(ErrorMessage:="A {0} is required.")>
    <StringLength(120, ErrorMessage:="{0} is too long.")>
    Public Property gNAME As String

    <DisplayName("Description")>
    <Required(ErrorMessage:="A {0} is required.")>
    <StringLength(2000, ErrorMessage:="{0} is too long.")>
    Public Property gDESC As String

    Private _hdmtORG As hdmtORG
    Public Overridable Property hdmtORG As hdmtORG
        Friend Get
            Return _hdmtORG
        End Get
        Set(ByVal value As hdmtORG)
            _hdmtORG = value
        End Set
    End Property

End Class

Partial Public Class Organisation
    Public Id As Integer
    Public Name As String
End Class

我的控制器:

    Public Class GroupController
    Inherits System.Web.Mvc.Controller

    Private unitOfWork As UnitOfWork = New UnitOfWork()

    Function Index() As ViewResult
        PopulateOrgsDropDownList()
        Return View(Me.unitOfWork.GroupRepository.Get())
    End Function

    <GridAction()>
    Public Function AjaxSelect() As ActionResult
        Return View(New GridModel(Me.unitOfWork.GroupRepository.Get()))
    End Function

    Private Sub PopulateOrgsDropDownList(Optional selectedOrg As Object = Nothing)
        ViewData("orgs") = Me.unitOfWork.OrgRepository.Get() _
                            .Select(Function(o) New With {.Id = o.orgID, .Name   =o.orgNAME}) _
                            .OrderBy(Function(o) o.Name)
    End Sub

我的观点:

'declare the grid and enable features
Dim grid = Html.Telerik().Grid(Model) _
                    .Name("Grid") _
                    .DataKeys(Function(k) k.Add(Function(g) g.gID)) _
                    .Pageable() _
                    .Sortable() _
                    .Filterable() _
                    .ToolBar(Function(t) t.Insert()) _
                    .DataBinding(Function(dataBind) dataBind.Ajax() _
                                    .Select("AjaxSelect", "Group") _
                                    .Insert("Create", "Group") _
                                    .Update("Edit", "Group") _
                                    .Delete("Delete", "Group"))
'Add grid columns
grid.Columns(Function(columns) columns.Bound(Function(g) g.gNAME).Width(200))
grid.Columns(Function(columns) columns.Bound(Function(g) g.gDESC).Width(200))
grid.Columns(Function(columns) columns.Bound(Function(g) g.Organisation).Width(200))
grid.Columns(Function(columns) columns.Command(Function(s) {s.Edit().ButtonType(GridButtonType.BareImage), s.Delete.ButtonType(GridButtonType.BareImage)}).Width(65))
'Render the grid
grid.Render()  
function onEdit(e) {
    $(e.form).find('#Organisation').data('tDropDownList').select(function (dataItem) {
        return dataItem.Text == e.dataItem['Organisation'];
    });
}

部分观点:

@imports System.Collections
@imports Telerik.Web.Mvc.UI
@(Html.Telerik().DropDownList() _
      .Name("Organisation") _
      .BindTo(New SelectList(DirectCast(ViewData("orgs"), IEnumerable), "Id", "Name")))
@

我将不胜感激任何帮助。

非常感谢。

2 个答案:

答案 0 :(得分:0)

通过将_OrgDropDownListPartial创建为部分视图,您犯了错误。您应该将所有UiHint属性代码放在 Views \ Shared \ EditorTemplates 文件夹中。

在您的情况下,您应该将所有_OrgDropDownListPartial局部视图代码移动到Views \ Shared \ EditorTemplates_OrgDropDownListPartial.ascx文件中

答案 1 :(得分:0)

我明白你在谈论什么,但我想我错过了什么。

我需要在列中加载的组织类是:

Partial Public Class hdmtORG

    Public Property orgID As Integer
    Public Property orgNAME As String

    Private _hdmtGROUPs As ICollection(Of hdmtGROUP) = New HashSet(Of hdmtGROUP)
    Public Overridable Property hdmtGROUPs As ICollection(Of hdmtGROUP)
        Friend Get
            Return _hdmtGROUPs
        End Get
        Set(ByVal value As ICollection(Of hdmtGROUP))
            _hdmtGROUPs = value
        End Set
    End Property

End Class

对于我的模型,我正在按照这个例子,使用工作单元和通用存储库:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

关键是我理解我没有正确加载我的组织(hdmtORG)以在我的组(hdmtGROUP)Grid列上显示它们。我必须在hdmtGROUP中私有我的属性hdmtORG以避免循环引用错误。当我进行查询时,如果我添加参数“includes hdmtORG”,ORG会加载到我的模型中,我可以在视图中看到它作为Model(0).hdmtORG.orgNAME。但是没有办法在grid.Columns上显示它们。当我尝试这样的事情时

grid.Columns(Function(columns) columns.Bound(Function(g) g.hdmtORG.orgNAME))

我无法访问,因为hdmtORG的GET不可访问,并且它要求我输入字符串值。