c# - 在 ASP.net MVC 4 中使用局部 View

标签 c# asp.net-mvc razor asp.net-mvc-4 partial-views

我最近开始研究 ASP.net MVC (4),但我无法解决我遇到的这个问题。我敢肯定,当您知道时,这很容易。

我实际上是在尝试在我的索引 View 中执行以下操作:

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

所以我想我需要一个局部 View ,并且我创建如下 (_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>
}

在我原来的索引 View (Index.cshtml) 中,我试图呈现这个局部 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>

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

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

我的 NotesController 看起来像这样:

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");
}

我认为这与索引 View 以不同方式使用模型这一事实有关,如在@model IEnumerable 中,但无论我如何更改它,使用 RenderPartial、RenderAction、将 ActionResult 更改为 ViewResult 等,我都可以'让它开始工作。

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

最佳答案

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

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

这是因为部分 View 需要一个 Note 但正在传递父 View 的模型,即 IEnumerable

关于c# - 在 ASP.net MVC 4 中使用局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934671/

相关文章:

c# - 如何在 SQL 中组合名字和姓氏并使用 LIKE 进行搜索

c# - WPF 中的高效位图操作 (C#)

c# - 允许将空日期时间输入数据库吗?

razor - 更新参数值后未调用 Blazor OnParametersSet

asp.net-mvc - 使用带有半相对URL的Url.Content

c# - 以下哪一种是在 Controller 外部将 View 渲染为字符串的首选方式?

asp.net-mvc - 针对各种应用程序的 Autofac 模块扫描

c# - MVC ajax 不工作

asp.net-mvc - 如何刷新 AJAX 帖子上的 Razor View

asp.net-mvc-3 - 由于 session 而导致单元测试失败