-1

I am new to .Net and MVC and I seem to hit a problem that I can´t fix. I am trying to follow a couple of tutorials but I have failed when it comes to partial views. I looked up a lot of answers here and on different websites and still nothing works. I can´t figure out what I am doing wrong.

This is my partial controller:

 public class PartialController : Controller
  {
    public ActionResult _PartialView()
    {
        var model = new Partial();
        model.Count = 2;

        return PartialView(model);
    }
  }

And this is my Partial View:

@model WebApplication1Tutorial.Models.Partial
<div>
The magic number is: 
@Html.DisplayFor(model => model.Count) 
</div>

I call this view in Index.cshtml by the following command:

<div>
@Html.Partial("/Views/Partial/_PartView.cshtml",new Partial())
</div>

And finally this is the model:

public class Partial
{
    public int Count { get; set; }
}

Everything that I tried comes up with the webpage displaying the text and the number 0. I really have no idea what else to do. I know it is something easy but I can´t see it.

Thanks for the help.

Edit. The code for the index view:

@model MovieGenreViewModel
@{
  ViewData["Title"] = "Index";
 }

<h2>Index</h2>

<p>
 <a asp-action="Create">Create New</a>
</p>

<form asp-controller="Movies" asp-action="Index" method="get">
<p>
    <select asp-for="movieGenre" asp-items="Model.genres">
        <option value="">All</option>
    </select>

    Title: <input type="text" name="SearchString" >
    <input type="submit" value="Filter" />
</p>
</form>

<table class="table">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.movies[0].Genre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.movies[0].Price)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.movies[0].ReleaseDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.movies[0].Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.movies[0].Rating)
        </th>
        <th></th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model.movies) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReleaseDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </td>
        <td>
            <a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
            <a asp-action="Details" asp-route-id="@item.ID">Details</a> |
            <a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
        </td>
    </tr>
}
</tbody>
</table>

<div>
@{ Html.RenderAction("_PartialView"); }
</div>
2
  • I saw that a lot of people were saying to us @Html.Action, the only problem i have is when I try it says that the model for index doesn't contain a definition for Action. Commented Feb 27, 2017 at 10:51
  • Thank you for identifying the issue and helping me solve it. sorry to have wasted your time. Commented Feb 27, 2017 at 11:14

1 Answer 1

0

When you pass your partial like with the @Html.Partial, you also pass a new Model and the _PartialView() method is not invoked so the model is not populated with the value 2. You could use @Html.Action helper:

<div>
    @Html.Action("/Views/Partial/_PartView.cshtml")
</div>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.