データ用にEntity Frameworks & SQL Serverを使用する基本Webフォームプロジェクトがあります。 GridViewをテーブルに接続しようとしていますが、何らかの理由でUpdateMethodが呼び出されません。 「更新」リンクがクリックされたときにポストバックを行いますが、gvNames_UpdateItemには決して行きません。どんな助けでも大歓迎です。モデルバインディングUpdateMethodが呼び出されないGridView
<asp:GridView ID="gvNames" runat="server" SelectMethod="gvNames_GetData" UpdateMethod="gvNames_UpdateItem"
DeleteMethod="gvNames_DeleteItem" CssClass="table table-hover table-striped"
UseAccessibleHeader="true" ItemType="CountyPMFraudData.NotifyName" AutoGenerateColumns="false" DataKeyNames="NotifyNamesID"
ShowHeaderWhenEmpty="true" OnPreRender="gvNames_PreRender" >
<Columns>
<asp:DynamicField DataField="NotifyNamesID" Visible="false" />
<asp:DynamicField DataField="UserID" Visible="false" />
<asp:DynamicField DataField="CorpFlag" HeaderText="Business" />
<asp:DynamicField DataField="FirstName" />
<asp:DynamicField DataField="LastName" HeaderText="Last Name/Business Name" />
<asp:DynamicField DataField="Active" HeaderText="Active" />
<asp:CommandField ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true"/>
</Columns>
</asp:GridView>
そして、これは背後にあるコードです:
public void gvNames_DeleteItem(int notifyNamesID)
{
using (var dbContext = new CountyPMFraudEntities())
{
var item = new NotifyName { NotifyNamesID = notifyNamesID };
dbContext.Entry(item).State = EntityState.Deleted;
try
{
dbContext.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
ValidationError.Display(String.Format("Item with id {0} no longer exists in the database.", notifyNamesID));
}
}
}
public IEnumerable gvNames_GetData()
{
List<NotifyName> names = null;
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
using (var dbContext = new CountyPMFraudEntities())
{
names = dbContext.NotifyNames.Where(NotifyName => NotifyName.UserID == _userID).ToList();
}
}
return names;
}
public void gvNames_UpdateItem(int notifyNamesID)
{
using (var dbContext = new CountyPMFraudEntities())
{
var name = dbContext.NotifyNames.Find(notifyNamesID);
if (name == null)
{
ValidationError.Display(String.Format("Item with id {0} was not found", notifyNamesID));
return;
}
TryUpdateModel(name);
if (ModelState.IsValid)
{
dbContext.SaveChanges();
}
}
}
protected void gvNames_PreRender(object sender, EventArgs e)
{
try
{
gvNames.HeaderRow.TableSection = TableRowSection.TableHeader;
}
catch (Exception)
{
//Just ignore becuase there is no data.
}
}