Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using partial views in ASP.net MVC 4

I have recently started playing around with ASP.net MVC (4), but I can't wrap my head around this one issue I'm having. I'm sure it's easy when you know it.

I'm essentially trying to do the the following in my Index view:

  1. List the current items in the database of type "Note" in the Index view (that's easy)
  2. Creating new items in the same Index view (not so easy).

So I figured I needed a partial view, and that I have created as following (_CreateNote.cshtml):

@model QuickNotes.Models.Note @using (Html.BeginForm()) { @Html.ValidationSummary(true)  <fieldset>     <legend>Note</legend>      <div class="editor-label">         @Html.LabelFor(model => model.Content)     </div>     <div class="editor-field">         @Html.EditorFor(model => model.Content)         @Html.ValidationMessageFor(model => model.Content)     </div>     <p>         <input type="submit" value="Create" />     </p> </fieldset> } 

In my original Index view (Index.cshtml) I'm trying to render this partial view:

@model IEnumerable<QuickNotes.Models.Note>   @{     ViewBag.Title = "Personal notes"; }  <h2>Personal notes</h2>  <p>     @Html.ActionLink("Create New", "Create") </p>  <table>     <tr>         <th>             @Html.DisplayNameFor(model => model.Content)         </th>         <th></th>     </tr>      @foreach (var item in Model) {         <tr>             <td>                 @Html.DisplayFor(modelItem => item.Content)             </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>     } </table>  <div>     @Html.Partial("_CreateNote") </div> 

(using: @Html.Partial("_CreateNote")) However. This doesn't seem to work, as I get the following error message:

Line 35:  Line 36: <div> Line 37:     @Html.Partial("_CreateNote"); Line 38: </div>  Source File: c:\Dropbox\Projects\workspace .NET MVC\QuickNotes\QuickNotes\Views\Notes\Index.cshtml    Line: 37   Stack Trace:    [InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.DbSet`1[QuickNotes.Models.Note]', but this dictionary requires a model item of type 'QuickNotes.Models.Note'.]    System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +405487 

My NotesController looks like this:

public ActionResult Index() {      var model = _db.Notes;      return View(model); }  // // GET: /Notes/Create  public ActionResult Create() {     return View(); }  // // GET: /Notes/_CreateNote - Partial view public ViewResult _CreateNote() {     return View("_CreateNote"); } 

I think it has to do with the fact that the Index view is using the model differently, as in @model IEnumerable, but no matter how I change it around, using RenderPartial, RenderAction, changing ActionResult to ViewResult etc, I can't get it to work.

Any tips would be greatly appreciated! Please let me know if you need any more information. I'd be happy to zip down the entire project if needed.

like image 976
Espen S. Avatar asked Dec 18 '12 14:12

Espen S.


People also ask

How many ways we can render partial view in MVC 4?

There are 5 different way of rendering a partial view.

How we can use partial view in MVC?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

Can we use multiple partial view in MVC?

The data will be fetched from database using Entity Framework and then multiple Partial Views will be rendered inside For Loop using the @Html. Action function in ASP.Net MVC Razor. In this article I will explain with an example, how to render multiple Partial Views inside Loop using Model in ASP.Net MVC Razor.

What is advantage of partial view in MVC?

Advantages of Partial View in ASP.net MVC: using Partial View in ASP.NET MVC has following advantages: Enhances reusability by packaging up common website code instead of repeating the same in different pages. Easy to maintain. Changes in future are simple to accommodate.


2 Answers

Change the code where you load the partial view to:

@Html.Partial("_CreateNote", new QuickNotes.Models.Note()) 

This is because the partial view is expecting a Note but is getting passed the model of the parent view which is the IEnumerable

like image 152
Richard Dalton Avatar answered Oct 16 '22 02:10

Richard Dalton


You're passing the same model to the partial view as is being passed to the main view, and they are different types. The model is a DbSet of Notes, where you need to pass in a single Note.

You can do this by adding a parameter, which I'm guessing as it's the create form would be a new Note

@Html.Partial("_CreateNote", new QuickNotes.Models.Note()) 
like image 23
harriyott Avatar answered Oct 16 '22 03:10

harriyott