c# - WebForms 模型绑定(bind) : Automatically map updated values to Item

标签 c# asp.net webforms model-binding

我目前正在开发一个小型虚拟应用程序,以了解 WebForms 中新的(很棒的)模型绑定(bind)。我已经成功地在 ListView 和 FormView 中列出了数据,但是我一直坚持更新数据。

我有一个非常简单的 FormView 控件:

<asp:FormView runat="server" ID="formViewOrderDetail" 
    ItemType="ModelBindingDummy.Models.Order" DataKeyNames="Id"
    SelectMethod="GetOrder" UpdateMethod="UpdateOrder">
    <ItemTemplate>
        <p>Supplier Order Number: <%#: Item.SupplierOrderNumber %></p>
        <asp:LinkButton Text="Edit" runat="server" ID="linkEdit"
                        CommandName="Edit" />
    </ItemTemplate>
    <EditItemTemplate>
        <p>Supplier Order Number: 
            <asp:TextBox runat="server" ID="SupplierOrderNumber" 
                         Text='<%#: Item.SupplierOrderNumber %>' />
         </p>
        <asp:LinkButton Text="Cancel" runat="server" CommandName="Cancel" />
        <asp:LinkButton Text="Save" runat="server" CommandName="Update" />
    </EditItemTemplate>
</asp:FormView>

我的 UpdateOrder方法如下所示:

public void UpdateOrder(int id)
{
    ModelBindingDummy.Models.Order item = GetOrder(id);
    if (item == null)
    {
        ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
        return;
    }            
    TryUpdateModel(item);

    //All data in the app is in-memory, so no need to actually update
    //the item as it is only a reference
}

现在我的问题是 TryUpdateModel 不起作用。它只是忽略了 TextBox 中的值 SupplierOrderNumber

我已经尝试向更新方法添加一个参数,[Control] string supplierOrderNumber ,虽然这行得通,但我真的很想看到一种自动映射该值的方法。

我还看到了使用 <asp:DynamicEntity /> 的解决方案控制,但这似乎有点矫枉过正。

所以我的问题是:是否有可能获得 TryUpdateModel映射 TextBox 值的方法,还是我需要一直使用 DynamicEntities控制,还是有任何其他奇特的解决方案?

谢谢!

最佳答案

您需要使用 BindItem.SupplierOrderNumber 而不是 Item.SupplierOrderNumber

关于c# - WebForms 模型绑定(bind) : Automatically map updated values to Item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18515374/

相关文章:

C# Lotus Notes - 已发送的邮件不在

c# - 用户 'san' 在 Azure 上的 SQL Server 中登录失败

c# - 使用 Newtonsoft 的 JSON C# 问题

javascript - 将 Iframe 中的内容复制到 div

c# 如何以编程方式创建表单的副本?

c# - 在 ASP 网页中分页?

asp.net - 使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证

c# - asp.net json 序列化程序将反斜杠 "\"添加到我的属性

asp.net - 自动生成的表单值

c# - 将 12 小时时间转换为时间跨度 C#