Partial Views with WebFu

Using partial views with WebFu is not necessary if you only use server controls, see below "Server Controls"

Most partial views in WebFu will follow this recipe:
  • They are UserControls, taking advantage of Visual Studio's markup editor and codebehind
  • They are used in an MVP fashion, where a partial view is strongly tied to its View Model
  • They do not use ViewState, rather, values come in from a postback to the page hosting the controls, those values are bound to an instance of a view model, then the view model is passed to the UserControl (partial view) when the page is rerendered by Web Forms.
  • Partial views should not call the WebFormsUtilities.WFPageUtilities.EnableClientValidation() method, if the page displaying them will already call this method. The page calling this method should do so after all of the controls have been rendered.

Partial views are rendered using WebFormsUtilities.WFUtilities.RenderControl() method, which will output a string.

WebFu will...
  • Auto-populate a UserControl's "Model" property if a model is supplied
  • Auto-populate a UserControl's "ViewBag" property, if an object is supplied
  • Check if the main page and the UserControl both have an 'Html' (HtmlHelper) property. If so, validation state and errors will be shared between them.

Sharing Validation

WebFu stores the model's validation state in a WFModelMetaData object. If the HTML helpers are used, the HTML helper has its own, publicly accessible, MetaData property. If a user performs a postback and validation errors are discovered, they will be stored in this metadata, so that later the HTML helpers on the page can output <input> and other tags with proper error styling and information.
When this metadata is shared between the page and its partial views (UserControls), the partial views can also output the proper validation error styling and information.

If using WebFu's HTML Helper's, and both page and UserControl have an HtmlHelper public property named "Html", sharing of metadata is automatic when the RenderControl() method is called and the page instance hosting the control is supplied.

Server Controls

The WebFu DataAnnotationValidatorControl uses Web Form's built-in page validation so that properties like Page.IsValid can be used just like any other Web Forms page. User Controls can use DataAnnotationValidatorControl's, and checking for validation works just like any Web Forms page.

Last edited Aug 14, 2013 at 3:55 AM by aikeru, version 1


No comments yet.