.NET 4.5 Gridview with DropDownList in EditItemTemplate UpdateMethod

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


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"
            <asp:TemplateField HeaderText="Proizvajalec">
                    <asp:Label ID="lblManufacturer" Text='<%# Item.Manufacturer.Title %>' runat="server" />
                    <asp:DropDownList ID="ddlManufacturers" runat="server" ItemType="Entities.Models.Manufacturer" SelectMethod="GetManufacturers" SelectedValue='<%# Item.Manufacturer.ManufacturerId %>'
                        DataTextField="Title" DataValueField="manufacturerId" OnCallingDataMethods="ddlManufacturers_CallingDataMethods">                            
            <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" />
        <PagerStyle CssClass="gridview_pager" />

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));
        if (context.ModelState.IsValid)
            // Save changes here

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; }

    public string Title { get; set; }

    public ManufacturerType ManufacturerType { get; set; }

public class ManufacturerType
    public int ManufacturerTypeId { get; set; }        

    public string Title { get; set; }

    public Manufacturer Manufacturer { get; set; }

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.

3/25/2015 8:20:28 PM

Related Questions


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