Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing partial views in ASP.net MVC 4
    primarykey
    data
    text
    <p>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.</p> <p>I'm essentially trying to do the the following in my Index view:</p> <ol> <li>List the current items in the database of type "Note" in the Index view (that's easy)</li> <li>Creating new items in the same Index view (not so easy). </li> </ol> <p>So I figured I needed a partial view, and that I have created as following (_CreateNote.cshtml):</p> <pre><code>@model QuickNotes.Models.Note @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Note&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Content) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Content) @Html.ValidationMessageFor(model =&gt; model.Content) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } </code></pre> <p>In my original Index view (Index.cshtml) I'm trying to render this partial view:</p> <pre><code>@model IEnumerable&lt;QuickNotes.Models.Note&gt; @{ ViewBag.Title = "Personal notes"; } &lt;h2&gt;Personal notes&lt;/h2&gt; &lt;p&gt; @Html.ActionLink("Create New", "Create") &lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Content) &lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Content) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; &lt;div&gt; @Html.Partial("_CreateNote") &lt;/div&gt; </code></pre> <p>(using: @Html.Partial("_CreateNote")) However. This doesn't seem to work, as I get the following error message: </p> <pre><code>Line 35: Line 36: &lt;div&gt; Line 37: @Html.Partial("_CreateNote"); Line 38: &lt;/div&gt; 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 </code></pre> <p>My NotesController looks like this:</p> <pre><code>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"); } </code></pre> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload