我目前正在开发一个小型虚拟应用程序,以了解 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/