3

I'm currently working on a little dummy application to get my head around the new (awesome) model binding in WebForms. I've succesfully listed data both in a ListView and a FormView, however I'm stuck on updating the data.

I have a very simple FormView control:

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

My UpdateOrder method looks like this:

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
}

Now my problem is that the TryUpdateModel doesn't work. It simply ignores the value in the TextBox SupplierOrderNumber

I've tried adding a parameter to the update method, [Control] string supplierOrderNumber, and while that works, I'd really like to see a way to automatically map that value.

I've also seen solutions using the <asp:DynamicEntity /> control, but that seems somewhat overkill.

So my question is: Is it possible to get the TryUpdateModel method to map the TextBox value, or do I need to go all the way with the DynamicEntities control, or are there any other fancy solutions?

Thanks!

4

2 回答 2

6

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

于 2014-02-13T10:53:55.217 回答
0

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

还有一个像这样的问题有更好的解释,但我找不到。

于 2013-12-26T22:33:46.243 回答