Screencast: Refactoring with Partial Views in MVC

In this screencast, I show you how to create maintainable and readable ASP.NET MVC razor views by refactoring them into smaller, reusable blocks with partial views.

Download the code: Refactoring-with-Partial-Views-in-MVC-Kennedy.zip

3 thoughts on “Screencast: Refactoring with Partial Views in MVC

  1. Thank you so much for the Video, everything is so clear and straight forward, its really nice

    I was just wondering what if I wanted to pass my datat directly to the view

    When returning the partial view to an Index page it works just fine

    Index.cshtml

    @model IEnumerable
    @foreach (var item in Model) {

    @Html.Partial(“_mydata”, item)
    }

    _mydata.cshtml is in my Page4 views folder

    @Html.DisplayFor(modelItem => @Model.Page4Entry)

    @Html.DisplayFor(modelItem => @Model.TimePosted)

    But when I place my partial view in my shared folder
    @model IEnumerable
    @foreach (var item in Model) {
    @Html.Partial(“_mydata”, item)
    }

    I get this error
    The model item passed into the dictionary is of type ‘System.Collections.Generic.List`1[PassingData01.Models.Home]’, but this dictionary requires a model item of type ‘System.Collections.Generic.IEnumerable`1[PassingData01.Models.Page4]’.

    Your help will be very much appreciated

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s