MVP with Gridview Selected Row

446 Views Asked by At

I'm trying to implement MVP pattern (using webforms) for a project I'm working on, but I'm getting bogged down in what to do after an event, and in particular, one that originates from a gridview. Currently, view implements an interface so that presenter can call methods on the view.

My gridview contains what are essentially a number of cut down employee objects (although it does not know this), each with an ID that I'm already going to have to hide in the grid.

When certain buttons are pressed on the grid rows, differing events might occur (eg. delete, add to job, etc). The presenter needs to get access to the employee id in order to perform function (which will probably fire off a service layer command with said id). I'm trying to keep the View passive, but I think in this example it is impossible.

The way I see it, there are only small number of ways this can be done.

  1. on gridrow select, populate a "selected ID field" within View's state (NOT gridrow id), and fire notify presenter (event or presenter call). Presenter will then access this field. Select Event needs to infer ID from gridview.

  2. Pass Id direct to to presenter, either through parameter on presenter notify ( or Event Arguments if using events to notify presenter). Select Event needs to infer ID from gridview.

If this was a dropdown list it would be easy, as you could just use GetSelectedValue, and have a wrapper method around this (implementing the view interface method).

Hope you can help.

Thanks.

1

There are 1 best solutions below

0
On

Ok, in the end I had to designate a little logic to the UI, and did the following:

For any sort of complex list object that I wish to be populated by a grid (or something similar to) I am passing down a DTO version through the interface.
eg:

interface IPersonView {

SetPersonList(List<PersonDTO> personList);

event Action PersonDeleteClicked;

PersonDTO GetSelectedPerson()

void HidePersonId() }

  1. The UI takes this and populates the grid with it.

  2. There are methods available to the presenter to hide columns if it wishes (such as HideId column).

  3. On the firing of the GridView button event, my UI retrieves the selected row object, and convert it to my list PersonDTO, and stores the selected ID in its own state.

  4. I then fire the PersonDeleteClicked event.

  5. The presenter, listening for this event, now calls interface method:

    PersonDTO GetSelectedPerson()

  6. UI responds by fetching PersonDTO by using saved PersonSelectedID