asp.net - .NET 4.5 Gridview with DropDownList in EditItemTemplate UpdateMethod -
i have gridview list of manufacturertypes related manufacturers... each manufacturertype has manufacturer (2 tables -> 2 entities). when want update item manufacturertype wan`t possible update title entity manufacturertype change (manufacturer) relation if 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 , change load item , save changes sections below:
// id parameter name should match datakeynames value set on control public void updateitem(int manufacturertypeid, modelmethodcontext context) { manufacturertype item = null; // load item here item = _manufacturertyperepository.getallwithrelations(m => m.manufacturer) .where(x => x.manufacturertypeid == manufacturertypeid).singleordefault(); if (item == null) { // 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 call include manufacturertype entity include("manufacturer") ... current manufacturertype, , tryupdatemodel method change title of manufacturertype (if change on edit in gridview), manufacturer stays same... have try put dropdownlist control-parameter updatemethod like
public void updateitem(int manufacturertypeid, [control] manufacturerid, modelmethodcontext context)
but null... don`t know how transfare value dropdownlist updateitem method (updateitem not in codebehind of manufacturertypes.aspx in busines logic layer) manufacturertypebl.cs...
i didn`t find solution use new features itemtype in gridview , select method , selectmethod in dropdownlist etc...
maybe need change updateitem method old fasioned way onupdating method , read values dropdown , lather call busines logic layer 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; } }
from can understand,
in edititemtempalte set selectedvalue='<%# bind("manufacturerid")%>'. i.e. property name manufacturerid in manufacturertype entity.
also, should not required fetch relation (in select query). if fetch manufacturertype item, , use correct property name in edittemplate controls, should work fine.
if can post model classes (manufacturer , manufacturertype) more helpful understand issue.
Comments
Post a Comment