使用ASP.net MVC 4中的部分视图

时间:2021-04-25 03:38:16

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.

我最近开始尝试使用ASP.net MVC(4),但是我不能完全理解我遇到的这个问题。我相信当你知道的时候,这很容易。

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. 在索引视图中列出“注意”类型的数据库中的当前项(这很简单)
  3. Creating new items in the same Index view (not so easy).
  4. 在同一个索引视图中创建新项(不是很容易)。

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

所以我想我需要一个局部视图,我创建了如下(_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:

在我最初的索引视图(Index.cshtml)中,我试图呈现这个部分视图:

@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:

(使用:@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

My NotesController looks like this:

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

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.

我认为这与索引视图使用模型的方式不同,如@model IEnumerable,但不管我如何改变它,使用RenderPartial, RenderAction,更改ActionResult到ViewResult等等,我都无法让它工作。

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.

如有任何建议,我们将不胜感激!如果你需要更多的信息,请告诉我。如果需要的话,我很乐意把整个项目拉下来。

2 个解决方案

#1


124  

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

这是因为部分视图期望得到一个注释,但它传递的是父视图的模型IEnumerable

#2


38  

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.

将相同的模型传递给部分视图,就像传递给主视图一样,它们是不同的类型。该模型是一个Notes的DbSet,您需要在其中传递一个注释。

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())

#1


124  

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

这是因为部分视图期望得到一个注释,但它传递的是父视图的模型IEnumerable

#2


38  

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.

将相同的模型传递给部分视图,就像传递给主视图一样,它们是不同的类型。该模型是一个Notes的DbSet,您需要在其中传递一个注释。

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())