【问题标题】:Telerik MVC grid, unable to make a dropdownlist on Ajax editingTelerik MVC 网格,无法在 Ajax 编辑上创建下拉列表
【发布时间】:2012-01-21 21:03:11
【问题描述】:

我正在尝试使用 Ajax 编辑实现 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")))
@

如果有任何帮助,我将不胜感激。

非常感谢。

【问题讨论】:

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


    【解决方案1】:

    您将 _OrgDropDownListPartial 创建为局部视图是错误的。您应该将所有 UiHint 属性代码放在 Views\Shared\EditorTemplates 文件夹中。

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

    【讨论】:

    • 嗨,马哈茂德,非常感谢!它现在可以工作,但仍然存在一个问题:按下编辑按钮时下拉列表显示得很好,它会更新数据库中的值,但我无法使用字段值填充组织列。我在模型中创建了一个部分类,但它不起作用,你知道是什么问题吗? (我已经用最后一次尝试更新了代码)
    • 你的意思是当你按下“编辑”按钮时,相关组织没有在DropDownList中选择?事实上,你不需要做任何事情!注意! :如果您的组合框绑定到项目列表(具有键/值对,如 (OrgId,OrganizationName) ),您的模型应该保留 OrgId 字段而不是 OrganizationName!之后,telerik DropDownList 会在您切换到编辑模式时自动选择相关项目
    • 我的意思是当我加载网格时,关于组织的列出现没有任何信息。当我按下编辑按钮时,下拉列表出现但不在相关组织上,它只显示下拉列表。正如我之前所说,我将我的公共财产组织添加为字符串,并将其设为具有 Id 和名称的部分类(参见模型),但仍然无法正常工作。
    【解决方案2】:

    我明白你在说什么,但我想我错过了一些东西。

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

    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) 网格列上显示它们。我不得不在 hdmtGROUP 中将我的财产 hdmtORG 设为私有,以避免循环引用错误。当我进行查询时,如果我添加参数“包括 hdmtORG”,则 ORG 会加载到我的模型中,我可以在视图中看到它为 Model(0).hdmtORG.orgNAME。但是没有办法在 grid.Columns 上显示它们。当我尝试这样的事情时

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2012-02-19
      • 1970-01-01
      相关资源
      最近更新 更多