没有调用带有模型绑定 UpdateMethod 的 GridView

GridView w/ model binding UpdateMethod not getting called

我有一个基本的 Web 窗体项目,使用实体框架和 SQL 服务器来获取数据。我正在尝试将 GridView 连接到 table,但由于某种原因没有调用 UpdateMethod。单击 "Update" link 但它永远不会到达 gvNames_UpdateItem 时,它会执行回发。任何帮助将不胜感激。

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

}

好的,我明白了。有两件事正在发生。首先,我错过了添加 "ValidationSummary" 和启用 ShowModelStateErrors 这样的:

<asp:ValidationSummary ID="vsHuman" ShowModelStateErrors="true" ValidationGroup="vgHuman" runat="server" ForeColor="Red" />

一旦我这样做了,我就意识到我遇到了一个错误 "The NotifyNamesID field is required."。有了这个我发现由于某种原因隐藏的"DynamicField"在返回服务器时不包含数据,所以我不得不将它更改为这样的TemplateField:

<asp:TemplateField Visible="false">
    <ItemTemplate>
        <asp:HiddenField runat="server" Value='<%#Eval("NotifyNamesID") %>' />
    </ItemTemplate>
</asp:TemplateField>

一旦我这样做了,事情就开始起作用了。希望这对以后的人有所帮助。