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

发布于 2024-12-28 12:23:43 字数 3599 浏览 1 评论 0原文

我正在尝试使用 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")))
@

我将不胜感激。

多谢。

I'm trying to implement a Telerik MVC grid with Ajax Editing. Basically, it's a class of groups, and every group belongs to an organisation, and I need to show on the edit mode a dropdownlist with the possible organisations to choose.

I've followed the tutorials and this forum but I cannot make it work.

This is my code:

Model:

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

My controller:

    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

My view:

'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'];
    });
}

And the partial view:

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

I would appreciate any help.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2025-01-04 12:23:44

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

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

You made wrong by creating _OrgDropDownListPartial as partial view. You should put your all UiHint attributed codes in Views\Shared\EditorTemplates folder.

In your case , you should move all your _OrgDropDownListPartial partial view codes into a Views\Shared\EditorTemplates_OrgDropDownListPartial.ascx file

夏有森光若流苏 2025-01-04 12:23:44

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

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

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

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

要点是我知道我没有正确加载我的组织(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 不可访问,它询问我为字符串值。

I understand what you're talking about but I guess I'm missing something.

The Organisation class I need to load in the column is this:

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

I'm following this example, with a Unit of Work and Generic Repository, for my model:

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

The point is that I understand I'm not loading properly my Organisations (hdmtORG) to show them on my groups (hdmtGROUP) Grid column. I had to make private my property hdmtORG in hdmtGROUP to avoid the circular reference error. When I make the query, if I add the parameter "including hdmtORG", the ORGs are loaded in my model, I can see it in the view as Model(0).hdmtORG.orgNAME. But there's no way to show them on the grid.Columns. When I try something like this

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

I cannot access because GET of hdmtORG is not accesible and it asks me for a string value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文