EF 6和ListView SelectMethod

时间:2013-11-26 03:00:49

标签: vb.net entity-framework listview

我在VS 2013中有一个Listview,我正在尝试将SelectMethod与EF 6和VB一起使用。列表视图的第一部分是:

<asp:ListView ID="ListView1" runat="server" selectmethod="ListView1_GetData" 
    InsertItemPosition="LastItem" 
    DataKeyNames="SectorId" 
    UpdateMethod="ListView1_UpdateItem" >

ListView1_GetData完美地填充列表 - 它只有3个字段。这是VB代码:

Private myentity As New RoutesEntities()
Public Function ListView1_GetData() As IQueryable(Of Sector)
    Return From myuserlist In myentity.Sectors Select myuserlist
End Function

列表视图的这一部分的“编辑”按钮显示了预期的列表:

<EditItemTemplate>
   <tr style="background-color:#008A8C;color: #FFFFFF;">
     <td>
       <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
         Text="Update" CausesValidation="False" />
       <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
         Text="Cancel" CausesValidation="False" />
     </td>
     <td>
       <asp:Label ID="SectorIdLabel1" runat="server" style= "width:40px" Text='<%#Eval "SectorId") %>' />
     </td>
     <td>
       <asp:TextBox ID="SectorTitleTextBox" runat="server" 
         Text='<%# Bind("SectorTitle") %>' />
     </td>
     <td>
       <asp:TextBox ID="SectorDescriptionTextBox" runat="server" 
         Text='<%# Bind("SectorDescription") %>' />
     </td>
         </tr>
 </EditItemTemplate>

以下是实际进行更新的方法的开始:

   ' The id parameter name should match the DataKeyNames value set on the control
Public Sub ListView1_UpdateItem(ByVal Sectorid As Integer)
    Dim theID As String
    theID = Sectorid
    MsgBox(theID)
End Sub

VS2013创建了sub,我用“Sectorid”替换了“id”。当我单击列表中的不同行并单击更新按钮时,我将转到此子,并且MsgBox中会显示正确的ID。从这一点来说,我完全不知道如何继续。我找不到与我正在尝试做的相关的VB教程。这个SelectMethod似乎是一个好主意,但似乎没有太多关于如何使用它的信息。任何有关VB教程的建议或指针都将非常感激。

更新 - 以下代码

非常感谢Imar我现在向前迈进,并且认为我会为可能有相同需求的其他人分享更新程序。请注意,我不是一个专业的程序员,当然其他人可以改进这些代码 - 但它对我有用,并为我网站中的其他页面提供了我想要的灵活性。这个页面只是第一次测试这个模型的好地方。

 Imports System.Linq
 Imports RoutesEntities
 Partial Class Management_SectorMaint
Inherits System.Web.UI.Page
Private myentity As New RoutesEntities()
Public Function ListView1_GetData() As IQueryable(Of Sector)
    Return From myuserlist In myentity.Sectors Select myuserlist
End Function

Protected Sub Page_Unload(sender As Object, e As EventArgs) Handles Me.Unload
    If myentity IsNot Nothing Then
        myentity.Dispose()
    End If
End Sub

Public Sub ListView1_UpdateItem(ByVal sectorid As Integer, ByVal sectortitle As String, ByVal sectordescription As String)

    Dim items As Sector = Nothing
    Dim sectoridnumber As Integer
    Dim mysectortitle As String
    Dim mysectordescription As String

    sectoridnumber = sectorid
    mysectortitle = sectortitle
    mysectordescription = sectordescription

    items = (From s In myentity.Sectors
                       Where s.SectorId = sectoridnumber
                       Select s).Single()
    If items Is Nothing Then
        MsgBox("Bad sectorID")
        Return
    End If
    items.SectorTitle = mysectortitle
    items.SectorDescription = mysectordescription

    TryUpdateModel(items)
    If ModelState.IsValid Then
        myentity.SaveChanges()
    End If
End Sub

结束班

以下是上述代码的“仅管理员”页面的部分屏幕截图:

http://www.bikex.net/sectorMaintPix.png

改进代码

Per Imar是一个清理版本:

  Public Sub ListView1_UpdateItem(sector As Sector)

    Dim items As Sector = Nothing
    items = (From s In myentity.Sectors
                       Where s.SectorId = sector.SectorId
                       Select s).Single()
    If items Is Nothing Then
        msglabel.Text = " Failed"      'jquery handles hide-show Div and using a client registered "isPostBack" Var
        Return
    End If
    items.SectorTitle = sector.SectorTitle
    items.SectorDescription = sector.SectorDescription

    TryUpdateModel(items)
    If ModelState.IsValid Then
        myentity.SaveChanges()
        msglabel.Text = " Successful"
    End If
End Sub

最终版本

非常感谢Imar的教练 - 这个版本完美无缺:

 <asp:ListView ID="ListView1" runat="server" selectmethod="ListView1_GetData"
    ItemType="Sector" 
    DataKeyNames="SectorId" 
    UpdateMethod="ListView1_UpdateItem"> 
<AlternatingItemTemplate>
   <tr style="background-color:#FFF8DC;">
     <td>.............

 Imports System.Linq
 Imports RoutesEntities
 Partial Class Management_SectorMaint
 Inherits System.Web.UI.Page
 Private myContext As New RoutesEntities()
 Public Function ListView1_GetData() As IQueryable(Of Sector)
    Return From myuserlist In myContext.Sectors Select myuserlist
 End Function

Public Sub ListView1_UpdateItem(sector As Sector)
    Dim item As Sector = (From s In myContext.Sectors
                     Where s.SectorId = sector.SectorId
                     Select s).SingleOrDefault()
    If item Is Nothing Then
        msglabel.Text = " Failed"
        Return
    End If
    TryUpdateModel(item)
    item.SectorTitle = String.Concat(sector.SectorTitle, " test") ' A change to "item" here goes to DB
    If ModelState.IsValid Then
        myContext.SaveChanges()
        msglabel.Text = " Successful"
    Else
        msglabel.Text = " Fail"
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您可以将ListView的ItemType属性设置为您实体的类型。这使得控件强类型化。然后,您可以将UpdateItem方法中的Sectorid属性从int更改为您的类型(扇区,用户,您有什么)。然后,ASP.NET将使用来自表单的数据填充您的实例。

作为替代方案,您还可以调用TryUpdateModel并传入模型类的新实例,然后填充该实例。

可在此Model Binding tutorial中找到更多详细信息。