5

I am at my first MVC project. I want to create a page with a header and in this header to be placed a partial view with category list.

This is what I did so far: I created the master page (_Home.cshtml). Than in Shared folder I created a View (Category.cshtml). See my picture. enter image description here

My Category.cshtml content:

@model IEnumerable<ArtSchool.Models.Category>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Visible)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Visible)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
</tr>

}

My master page file:

@{
Layout = null;
}

<!DOCTYPE html>

 <html>
 <head>
<meta name="viewport" content="width=device-width" />
<title>_Home</title>
</head>
<body>
<div>

    @Html.Partial("ASCX/Header")

    @Html.Partial("Category")

    @RenderBody()
</div>

When I run the project I got the error:

enter image description here

I know that is a newbie question but it's my first MVC project. Thanks!

5
  • You need to pass a Model with this call @Html.Partial("Category"). You should call a Controller/Action in which this model gets generated and then return the partial. Commented Feb 12, 2014 at 13:39
  • what have you done in your actions?? you should render partial view in your controller Commented Feb 12, 2014 at 13:46
  • Are you sure? In my project I have nothing more he showed. I'm not passing any additional data,calling controler,returning partial and everything works Commented Feb 12, 2014 at 13:47
  • @szpic, is your partial view strongly typed? If so, then any reference to the Model will yield the same error. Commented Feb 12, 2014 at 13:47
  • @AndreiV Yes. In my vs strongly typed is default. There is no checkbox create strongly Typed etc Commented Feb 12, 2014 at 13:53

2 Answers 2

7

Solution 1

If you want to use partial view you need to pass model to this helper this way

@Html.Partial("Category", CategoryModel)

before you pass this model you have to fill it with some data.

Solution 2

also you can use @Html.Action() Method with name of ActionResult method which will return partial view for you.

for example:

  @Html.Action("GetCategories", "ControllerName")

  public ActionResult GetCategories() {
    // fill some data for your model here
    return PartialView("Category", model);
  }
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to interpret those partials as some static sections inside your HTML, then I would suggest you to call Html.Action() which returns your partials:

@Html.Action("GetPageHeader","Home")
@Html.Action("GetPageCategories","Home")

HomeController

[HttpGet]
public ActionResult GetPageHeader()
{
   return PartialView(@"~/Views/Shared/_PageHeader.cshtml");
}

[HttpGet]
public ActionResult GetPageCategories()
{
   var categories = databaseContext.GetAllCategories(); //Get your categs
   return PartialView(@"~/Views/Shared/_Categories.cshtml",categories);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.