User Interface Composition in ASP.NET MVC

How could you support external components in an ASP.NET MVC view?

What I mean? Think of it as an “every page login window” or “iGoogle.” This is material that should be located in specific places that are external to each controller / view.

One approach to this would be to add components to the view as follows:

<% foreach (var component in GetComponents()) {%>
    <%= Html.RenderPartial(component.ViewName, component.ViewData)%>
<%} %>

In the example above, I'm looking for a good way to have the view and views supplied by each component controller, rather than the view controller on which they are displayed. Any completely different solution that you can offer is also of interest. Filters, WebForms, etc.

Update: . I will try to explain what I am trying to wrap myself around as an example. I will pick up the login.

In a typical webforms application, this could be a user control that retrieves the relevant data in the page lifecycle load event and updates some user interface elements. After clicking on the page, a response will be published, and we can act on the published information in the click event in the same user control.

ASP.NET MVC, , , , , , . , . , - .

, , , / .

+5
4

:

: BaseController, , , ViewData [ "loginbox" ] = loginBoxData, .

<% foreach (var component in GetComponents()) {%>
        <%= Html.RenderPartial(component.ViewName, ViewData[component.Name])%>
<%} %>

, , , , , .

TWO:

MVC Futures DLL, .

web.config, : Html.RenderAction("blah") - , , HTML.

+5

- .

ASP.NET MVC Framework Beta . / .

, , , . Index HomeController, Home/Index.aspx.

:

<asp:ContentPlaceHolder ID="Holder1" runat="server" />

:

<asp:Content ID="Content1" ContentPlaceHolderID="Holder1" runat="server">
  <p>This text goes to "Holder1" content place holder and
     I can access the ViewData.</p>
  <p><%= ViewData["Message"] %>
</asp:Content>

asp:ContentPlaceHolder asp:Content . , :

<asp:ContentPlaceHolder ID="Holder1" runat="server" />
<asp:ContentPlaceHolder ID="Holder2" runat="server" />

... :

<asp:Content ID="Content1" ContentPlaceHolderID="Holder1" runat="server">
  <p>This text goes to "Holder1" content place holder</p>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Holder2" runat="server">
  <p>This text goes to "Holder2" content place holder</p>
</asp:Content>

, . , Wekeroad . :

<%=Html.RenderUserControl("~/UserControls/UserList.ascx")%>

, .

<%=Html.RenderUserControl("~/UserControls/UserList.ascx",ViewData.Users, new {GroupID=2})%>
+2

, . . .

, . : "... , , , ."

+1

, :

CQRS ICommand , , .

: https://github.com/ncqrs/ncqrs/blob/master/Framework/src/Ncqrs/Commanding/ServiceModel/CommandService.cs

- , , ?

1 1 IViewModelExecutor?

ViewModel, ViewModels, , , :

service.Load(new List<IViewModelExecutor>{UserinfoViewModel, UserAccountViewModel });

readmodel , . Html.RenderPartial("subviewname", Model.SubViewModel)

, .

, ajax, , UserAccount . , JUST .

service.Load(new List<IViewModelExecutor>{UserAccountViewModel })

, - , . obv.

, .

And using content negotiation, we can serve HTML, JSON, or XML fragments depending on the HTTP headers in the request.

0
source

All Articles