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 comments

  • 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

Submit a comment