What is a good way to build sub views with logic in ASP.NET MVC 3 -


we're building business application using microsoft asp.net mvc 3. views becoming complex seems reasonable divide them 2 or more separate views, separate controllers , models. (reuse reason wanting build separate views.)

which method recommend achieving such separation in asp.net mvc?
partial views seems obvious answer, see it, partial views "out of box" has little support separate controllers.

the best solution we've found far this, using
html.renderaction("<view initialization action method>")
method explained here: http://www.primaryobjects.com/cms/article129.aspx

this post mentions renderaction method: asp .net mvc correct usercontrol architecture

do see weaknesses approach?
recommend better, easier ways of achieving this?

take "post detail" view. composite view displays both "post summary" , "post comments".

taking partial approach, you'd end with:

public class postdetailmodel {     postsummarymodel summary { get;set; }     postcommentsmodel comments { get;set; } } 

and view:

<div id="post_detail">     @html.partial("summary", model.summary)      <ul class="comment-list">     @foreach(var comment in model.comments)     {         <li>@html.partial("comment", comment)</li>     }     </ul> </div> 

this means postcontroller.detail method responsible constructing postsummarymodel, constructing postcommentsmodel , selecting partials use render each.

if had following model:

public class postlistmodel {     icollection<postsummarymodel> posts { get;set; } } 

you have 2 actions responsible constructing postsummarymodel , knowing partial use. if application isn't structured, might lead duplicate data access/model mapping code. if delegate , abstract model construction re-usable model factories (that called both actions) minimise risk.

one other hand, taking html.action approach model becomes:

public class postdetailmodel {     int postid { get;set; } } 

and view:

<div id="post_detail">     @html.action("summary", new { model.postid })      @html.action("comments", new { model.postid })  </div> 

it can left "summary" , "comments" actions construct own model , select view.

there ever slight performance hit in choosing html.action approach because asp.net mvc has go through whole process of model-binding, executing action filters, validating, etc wouldn't use html.action display items in sufficiently long list view. creating composite view can clean way stitch half dozen or existing views.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -