在 ASP.net MVC 4 中使用局部视图 [英] Using partial views in ASP.net MVC 4

查看:36
本文介绍了在 ASP.net MVC 4 中使用局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始玩 ASP.net MVC (4),但我无法解决我遇到的这个问题.我相信当你知道它时很容易.

我基本上是在我的索引视图中尝试执行以下操作:

  1. 在索引视图中列出数据库中Note"类型的当前项目(这很简单)
  2. 在同一个索引视图中创建新项目(不是那么容易).

所以我想我需要一个局部视图,并且我已经创建如下(_CreateNote.cshtml):

@model QuickNotes.Models.Note@using (Html.BeginForm()) {@Html.ValidationSummary(true)<字段集><legend>注意</legend><div class="editor-label">@Html.LabelFor(model => model.Content)

<div class="editor-field">@Html.EditorFor(model => model.Content)@Html.ValidationMessageFor(model =>model.Content)

<p><输入类型=提交"值=创建"/></p></fieldset>}

在我原来的索引视图 (Index.cshtml) 中,我试图呈现这个局部视图:

@model IEnumerable@{ViewBag.Title = "个人笔记";}<h2>个人笔记</h2><p>@Html.ActionLink("新建", "创建")</p><表格><tr><th>@Html.DisplayNameFor(model =>model.Content)</th><th></th></tr>@foreach(模型中的变量项目){<tr><td>@Html.DisplayFor(modelItem => item.Content)</td><td>@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |@Html.ActionLink("详情", "详情", new { id=item.ID }) |@Html.ActionLink("删除", "删除", new { id=item.ID })</td></tr>}<div>@Html.Partial("_CreateNote")

(使用:@Html.Partial("_CreateNote"))然而.这似乎不起作用,因为我收到以下错误消息:

第 35 行:第 36 行:<div>第 37 行:@Html.Partial("_CreateNote");第 38 行:</div>源文件:c:DropboxProjectsworkspace .NET MVCQuickNotesQuickNotesViewsNotesIndex.cshtml 行:37堆栈跟踪:[InvalidOperationException:传递到字典中的模型项的类型为System.Data.Entity.DbSet`1[QuickNotes.Models.Note]",但此字典需要类型为QuickNotes.Models.Note"的模型项.]System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +405487

我的 NotesController 如下所示:

public ActionResult Index(){var 模型 = _db.Notes;返回视图(模型);}////获取:/注释/创建公共 ActionResult 创建(){返回视图();}////GET:/Notes/_CreateNote - 局部视图公共 ViewResult _CreateNote(){return View("_CreateNote");}

我认为这与 Index 视图以不同方式使用模型有关,如@model IEnumerable,但无论我如何更改它,使用 RenderPartial、RenderAction、将 ActionResult 更改为 ViewResult 等,我都可以不让它工作.

任何提示将不胜感激!如果您需要更多信息,请告诉我.如果需要,我很乐意压缩整个项目.

解决方案

将加载分部视图的代码更改为:

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

这是因为局部视图需要一个注释,但是却传递了父视图的模型,即 IEnumerable

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:DropboxProjectsworkspace .NET MVCQuickNotesQuickNotesViewsNotesIndex.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.

解决方案

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

这篇关于在 ASP.net MVC 4 中使用局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆