2012-01-21 34 views
0

我試圖通過Ajax編輯實現Telerik MVC網格。基本上,它是一類組,並且每個組都屬於一個組織,我需要在編輯模式中顯示一個下拉列表,供可能的組織選擇。Telerik MVC網格,無法在Ajax編輯上做出下拉列表

我跟着教程和這個論壇,但我不能讓它工作。

這是我的代碼:

型號:

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"))) 
@ 

我將不勝感激任何幫助。

非常感謝。

回答

0

您通過將_OrgDropDownListPartial創建爲局部視圖而造成錯誤。您應該將您的所有UiHint歸因代碼放在Views \ Shared \ EditorTemplates文件夾中。

在你的情況,你應該將所有的_OrgDropDownListPartial局部視圖代碼到瀏覽\共享\文件EditorTemplates_OrgDropDownListPartial.ascx

+0

嗨馬哈茂德,非常感謝!它現在可以工作,但仍然存在一個問題:按下編輯按鈕時,下拉列表顯示正常,並且它更新了數據庫中的值,但我無法使用該字段的值填充組織列。我在模型中創建了一個部分類,但它不起作用,你知道有什麼問題嗎? (我已經用最後一次嘗試更新了代碼) – Scheveningen

+0

您的意思是,當您按下「編輯」按鈕時,相關組織不會在DropDownList中選擇?事實上,你不需要做任何事情! 注意! :如果您的組合框綁定到項目列表(使用類似(OrgId,OrganizationName)的鍵/值對),您的模型應該保留OrgId字段而不是OrganizationName!之後,當您更改爲編輯模式時,telerik DropDownList會自動選擇相關項目 –

+0

我的意思是,當我加載網格時,關於組織的列顯示沒有任何信息。當我按下編輯按鈕時,下拉列表會出現,但不會出現在相關的組織上,它只會出現在下拉列表中。正如我之前所說的,我將我的公共屬性組織添加爲字符串,並將其作爲ID和名稱的部分類(請參閱模型),但仍然不起作用。 – Scheveningen

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)網格列上顯示它們。我不得不在hdmtGROUP中私有我的財產hdmtORG以避免循環引用錯誤。當我進行查詢時,如果添加參數「包括hdmtORG」,ORG將加載到我的模型中,我可以在視圖中將其視爲Model(0).hdmtORG.orgNAME。但是沒有辦法在網格上顯示它們。列。當我嘗試這樣的事情

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

我無法訪問,因爲hdmtORG的GET不入店,它提示我輸入一個字符串值。

相關問題