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

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

问题描述

我最近开始与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. 列出在索引视图类型注的数据库的当前项目(这很容易)

  2. 创建在同一个索引视图(不那么容易)新项目。

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

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>
}

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

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>

(使用:@ Html.Partial(_ CreateNote))
然而。这似乎并没有工作,因为我得到了以下错误消息:

(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

我NotesController看起来是这样的:

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

我认为这与事实索引视图使用不同的模型,如@model IEnumerable的做,但是不管我怎么改变它周围,使用的RenderPartial,的RenderAction,改变的ActionResult到的ViewResult等,我可以'吨得到它的工作。

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.

任何提示将不胜AP preciated!
请让我知道如果你需要任何更多的信息。如果需要的话我会很高兴地压缩了整个项目。

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.

推荐答案

更改code,你加载局部视图为:

Change the code where you load the partial view to:

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

这是因为局部视图期待一个说明,但获得通过父视图的模型是了IEnumerable

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆