我試圖通過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")))
@
我將不勝感激任何幫助。
非常感謝。
嗨馬哈茂德,非常感謝!它現在可以工作,但仍然存在一個問題:按下編輯按鈕時,下拉列表顯示正常,並且它更新了數據庫中的值,但我無法使用該字段的值填充組織列。我在模型中創建了一個部分類,但它不起作用,你知道有什麼問題嗎? (我已經用最後一次嘗試更新了代碼) – Scheveningen
您的意思是,當您按下「編輯」按鈕時,相關組織不會在DropDownList中選擇?事實上,你不需要做任何事情! 注意! :如果您的組合框綁定到項目列表(使用類似(OrgId,OrganizationName)的鍵/值對),您的模型應該保留OrgId字段而不是OrganizationName!之後,當您更改爲編輯模式時,telerik DropDownList會自動選擇相關項目 –
我的意思是,當我加載網格時,關於組織的列顯示沒有任何信息。當我按下編輯按鈕時,下拉列表會出現,但不會出現在相關的組織上,它只會出現在下拉列表中。正如我之前所說的,我將我的公共屬性組織添加爲字符串,並將其作爲ID和名稱的部分類(請參閱模型),但仍然不起作用。 – Scheveningen