Terry

Terry

  • NA
  • 148
  • 0

Bind data to a DropDownList in Edit & Insert in ListView

Mar 26 2015 10:18 AM

Hi, 

    In my ASP.NET 4.5 WebForms app, In my ListView I am trying to bind DropDownList, but am not able to do it. Actually, the Model that is bind to ListView, has a Property of UnitdirectionId & on Edit & Insert Templates' I want to show the dropdown of all UnitDirection table.

Here is my EditTempalte :
            <EditItemTemplate>
               <tr>
                   <td>
                       <asp:TextBox ID="unitDirTxt" runat="server" Text='<%# Bind("UnitDirectionId") %>'></asp:TextBox>
                        <!-- <asp:DynamicControl runat="server" DataField="UnitDirectionId" ID="UnitDirectionId" Mode="Edit" />  -->
                       <asp:DropDownList ID="unitDirEditDrop" runat="server" ItemType="VincitoreCRMApplication.Models.UnitDirection"
                           DataTextField="DirectionTitle" DataValueField="UnitDirectionId" 
                           SelectMethod="GetUnitDirections"></asp:DropDownList>  
                   </td> 
              </tr>

            </EditItemTemplate>


In Code behind, 
        public IQueryable<UnitDirection> GetUnitDirections()
        {
            return _db.UnitDirections;
        }

Now, the Model of LsitView has the proerpty as follows :
        [Display(Name= "Unit Direction")]
        public int UnitDirectionId { get; set; }

        [ForeignKey("UnitDirectionId")]
        public virtual UnitDirection UnitDirection { get; set; }

Now, the UnitDirection Model :
    public class UnitDirection
    {
        [ScaffoldColumn(false)]
        public int UnitDirectionId { get; set; }

        [Required]
        [Display]
        public string DirectionTitle { get; set; }
    }

In db the UnitDirection table has 3 items. 

With the  above drop down, I believe I should atleast get list of all UnitDirection in drop down. But on that I am getting error. Then comes to set the selected item in Edit Template.

Can you please help me identify why I am getting this error ?? 
Any help is highly appreciated.

Thanks





Answers (1)