.NET 4.5 Gridview with DropDownList in EditItemTemplate UpdateMethod

.net-4.5 asp.net entity-framework-6 gridview

Question

I have Gridview with list of ManufacturerTypes that is related with Manufacturers... So each ManufacturerType has a Manufacturer (2 tables -> 2 entities). When I want to update item ManufacturerType I wan`t to be possible to update Title from Entity ManufacturerType but also change the (Manufacturer) relation if the dropdownlist selectedvalue change.

<asp:GridView ID="gvManufacturerTypes" runat="server" DataKeyNames="manufacturerTypeId"
        AutoGenerateColumns="false" GridLines="Vertical" CssClass="gridview"
        CellPadding="4" ItemType="Entities.Models.ManufacturerType" SelectMethod="GetManufacturerTypesWithParams" 
        UpdateMethod="UpdateItem" DeleteMethod="DeleteItem"
        AllowPaging="true" AllowSorting="true" PageSize="20" PagerSettings-FirstPageText="Prvi"
        PagerSettings-LastPageText="Zadnji" PagerSettings-Mode="NumericFirstLast"
        OnCallingDataMethods="gvManufacturerTypes_CallingDataMethods">
        <Columns>
            <asp:TemplateField HeaderText="Proizvajalec">
                <ItemTemplate>
                    <asp:Label ID="lblManufacturer" Text='<%# Item.Manufacturer.Title %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlManufacturers" runat="server" ItemType="Entities.Models.Manufacturer" SelectMethod="GetManufacturers" SelectedValue='<%# Item.Manufacturer.ManufacturerId %>'
                        DataTextField="Title" DataValueField="manufacturerId" OnCallingDataMethods="ddlManufacturers_CallingDataMethods">                            
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:DynamicField DataField="Title" />
            <asp:CommandField ShowEditButton="true" EditText="Uredi" UpdateText="Shrani" CancelText="Prekliči" ItemStyle-Width="80" />
            <asp:CommandField ShowDeleteButton="true" DeleteText="Izbriši" ItemStyle-Width="80" />
        </Columns>
        <PagerStyle CssClass="gridview_pager" />
    </asp:GridView>

It generates me UpdateItem method and I change the load item and save changes sections like below:

// The id parameter name should match the DataKeyNames value set on the control
    public void UpdateItem(int manufacturerTypeId, ModelMethodContext context)
    {
        ManufacturerType item = null;

        // Load the item here
        item = _manufacturerTypeRepository.GetAllWithRelations(m => m.Manufacturer)
            .Where(x => x.ManufacturerTypeId == manufacturerTypeId).SingleOrDefault();

        if (item == null)
        {
            // The item wasn't found
            context.ModelState.AddModelError("", String.Format("Proizvajalec z idjem {0} ne obstaja.", manufacturerTypeId));
            return;
        }
        context.TryUpdateModel(item);
        if (context.ModelState.IsValid)
        {
            // Save changes here
            _manufacturerTypeRepository.Save();
        }
    }

I also call the Include where I get the ManufacturerType entity with Include("Manufacturer") ... so I get the current ManufacturerType, and the TryUpdateModel method also change the Title of ManufacturerType (if I change it on edit in gridview), but Manufacturer always stays the same... I have also try to put the dropdownlist as control-parameter to UpdateMethod like

public void UpdateItem(int manufacturerTypeId, [Control] manufacturerId, ModelMethodContext context)

but it is always null... So I don`t know how to transfare value from dropdownlist to UpdateItem method (UpdateItem is not in codebehind of ManufacturerTypes.aspx but it is in busines logic layer) ManufacturerTypeBL.cs...

I didn`t find any solution for this that will use new features like ItemType in gridview and Select method and SelectMethod in dropdownlist etc...

Maybe I need to change the UpdateItem method with old fasioned way OnUpdating method and read the values from dropdown and lather call the busines logic layer with params?

EDIT - > add model classes (Manufacturer, ManufacturerType)

public class Manufacturer
{
    [Key, ForeignKey("ManufacturerType")]
    public int ManufacturerId { get; set; }

    [StringLength(255)]
    public string Title { get; set; }

    public ManufacturerType ManufacturerType { get; set; }
}

public class ManufacturerType
{
    [Key]
    public int ManufacturerTypeId { get; set; }        

    [StringLength(50)]
    public string Title { get; set; }

    public Manufacturer Manufacturer { get; set; }

}
1
3
3/27/2015 11:23:36 AM

Popular Answer

From what I can understand,

In the EditItemTempalte set SelectedValue='<%# Bind("ManufacturerId")%>'. i.e. Property name for the ManufacturerId in ManufacturerType Entity.

Also, it should not be required to fetch the relation (in the select query). If you fetch ManufacturerType item, and use correct property name in the EditTemplate controls, it should work fine.

If you can post your Model classes (Manufacturer and ManufacturerType) it will be more helpful to understand the issue.

0
3/25/2015 8:20:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow