views:

54

answers:

1

Hey there,

What is the best way to get a number of property values of a business object from the View to the Presenter in a WebFormsMvp page?

Bearing in mind this issue with DataSources: http://bit.ly/dtQhw0

Here is what i propose:

The scenario is, I have a business object called Quote which i would like to load form the database, edit and then save. The Quote class has heaps of properties on it. The form is concerned with about 20 of these properties. I have existing methods to load/save a Quote object to/from the database. I now need to wire this all together.

So, in the View_Load handler on my presenter i intend to do something like this:

public void View_Load(object sender, EventArgs e)
{
    View.Model.Quote = quoteService.Read(quoteId);
}

And then bind all my controls as follows:

<asp:TextBox ID="TotalPriceTextBox" runat="server"
    Text="<%# Model.Quote.TotalPrice %>" />

All good, the data is on the screen.

The user then makes a bunch of changes and hits a "Submit" button. Here is where I'm unsure.

I create a class called QuoteEventArgs exposing the 20 properties the form is able to edit. When the View raises the Submit button's event, I set these properties to the values of the controls in the code behind. Then raise the event for the presenter to respond to. The presenter re-loads the Quote object from the database, sets all the properties and saves it to the database.

Is this the right way to do this? If not, what is?

Cheers, Andrej.

+1  A: 

"A nicer way" (/alternative) is to make use of the 2-way binding, therefore what will be passed back to the Presenter for processing will be your Quote object.

This can be achieved through the use of an asp:FormView in conjunction with the mvp:PageDataSource that specifies an UpdateMethod and the Bind() method.

The WebFormsMVP sample project demonstrates this via the 'EditWidgetControl', including the methods required on the View code-behind file.

As an option your view can simply implement only the EditItemTemplate for asp:FormView making use of DefaultMode="Edit" on the FormView.

Sample Structure:

<asp:FormView DataSourceID="theSource" DefaultMode="Edit">
    <EditItemTemplate>
        <fieldset>
            <asp:TextBox id="totp" value='<%# Bind("TotalPrice") %>' runat="server" />
        </fieldset>
    </EditItemTemplate>
</asp:FormView>

<mvp:PageDataSource ID="theSource" runat="server"
    DataObjectTypeName="Your.NameSpace.Quote"
    UpdateMethod="UpdateQuote">
</mvp:PageDataSource>

Code-behind:

public void UpdateQuote(Quote q, Quote ori)
{
    OnUpdatingQuote(q, ori);
}

public event EventHandler<UpdateQuoteEventArgs> UpdatingQuote;

private void OnUpdatingQuote(Quote q, Quote ori)
{
   if (UpdatingUserGroup != null)
   {
       UpdatingUserGroup(this, new UpdateQuoteEventArgs(q, ori));
   }
}
Nick Josevski