在EditItemTemplate UpdateMethod中使用DropDownList的.NET 4.5 Gridview

时间:2015-03-17 06:37:19

标签: asp.net gridview .net-4.5 entity-framework-6

我有Gridview,其中包含与制造商相关的ManufacturerTypes列表......因此每个ManufacturerType都有一个制造商(2个表 - > 2个实体)。当我想要更新项目ManufacturerType时,我不可能从实体ManufacturerType更新标题,但如果下拉列表选择值发生变化,也可以更改(制造商)关系。

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

它生成了UpdateItem方法,我更改了加载项并保存了如下所示的更改部分:

// 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();
        }
    }

我还调用Include,其中我获得带有Include(“制造商”)的ManufacturerType实体...所以我获得当前的ManufacturerType,而TryUpdateModel方法也更改了ManufacturerType的标题(如果我在gridview中编辑时更改它) ),但制造商始终保持不变...我也尝试将下拉列表作为控件参数添加到UpdateMethod,如

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

但它始终为null ...所以我不知道如何将值从dropdownlist转换为UpdateItem方法(UpdateItem不在ManufacturerTypes.aspx的代码隐藏中,但它在busines逻辑层中)ManufacturerTypeBL.cs ...

我没有找到任何解决方案,它将使用gridview中的ItemType和下拉列表中的Select方法和SelectMethod等新功能......

也许我需要使用旧的fasioned方式OnUpdating方法更改UpdateItem方法并从下拉列表中读取值,并使用params调用业务逻辑层?

编辑 - &gt;添加模型类(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 个答案:

答案 0 :(得分:0)

据我所知,

在EditItemTempalte中设置SelectedValue =&#39;&lt;%#Bind(&#34; ManufacturerId&#34;)%&gt;&#39;。即ManufacturerType实体中ManufacturerId的属性名称。

此外,不应该要求获取关系(在选择查询中)。如果您获取ManufacturerType项,并在EditTemplate控件中使用正确的属性名称,它应该可以正常工作。

如果您可以发布您的模型类(制造商和制造商类型),那么理解该问题会更有帮助。