The code for the SearchForm:
public partial class DetailForm : ViewForm,
IView<NorthwindDataSet.ProductsRow>
{
[PublishEvent("OnBack")]
public event EventHandler OnBackEvent;
public DetailForm()
{
InitializeComponent();
}
// Callback from the Controller
private void OnProductLoaded(object sender, EventArgs e)
{
// Load the product details into controls
this.txtProductID.DataBindings.Add("Text",
this.ViewData.Model, "ProductID");
this.txtProductName.DataBindings.Add("Text",
this.ViewData.Model, "ProductName");
this.txtUnitPrice.DataBindings.Add("Text",
this.ViewData.Model, "UnitPrice");
this.txtCategoryID.DataBindings.Add("Text",
this.ViewData.Model, "CategoryID");
}
#region IView<NorthwindDataSet.ProductsRow> Members
public new ViewDataDictionary<NorthwindDataSet.ProductsRow> ViewData
{
get;
set;
}
#endregion
#region event handlers
private void menuItemBack_Click(object sender, EventArgs e)
{
if (OnBackEvent != null)
{
OnBackEvent(this, EventArgs.Empty);
}
}
#endregion
}
And now the DetailController:
public class DetailController : Controller<NorthwindDataSet.ProductsRow>
{
[PublishEvent("OnProductLoaded")]
public event EventHandler ProductLoaded;
public DetailController(IView<NorthwindDataSet.ProductsRow> view)
: base(view)
{
}
// Selected event from the view
private void OnBack(object sender, EventArgs e)
{
// Move back to the search form
NavigationService.GoBack();
}
protected override void OnInitialize(params object[] parameters)
{
this.view.ViewData.Model = parameters[0]
as NorthwindDataSet.ProductsRow;
// Notify the view
if (this.ProductLoaded != null)
{
this.ProductLoaded(this, EventArgs.Empty);
}
}
}
In the code above, we override the OnInitialize method into which the parameters from the SearchForm will passed via NagivationService class like so:
private void ShowDetailView(NorthwindDataSet.ProductsRow product)
{
DetailController controller = new DetailController(new DetailForm());
NavigationService.Navigate(controller, product);
}
This ShowDetailView method is a part of the SearchConroller which is called when a user indicates that a Product has been selected in the ListBox on the SearchForm:
// Selected event from the view
private void OnSelected(object sender,
DataEventArgs<NorthwindDataSet.ProductsRow> e)
{
this.ShowDetailView(e.Value);
}
I have also added the Northwind.sdf database as well as the generated strongly typed dataset to the modified sample. This is why you should probably notice the NorthwindDataSet.ProductRow data type which is used to pass the Product data between the controllers. As usual, you can download the sample code with the updated MVC framework.