C# 在 ASP.net MVC 4 中使用局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13934671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using partial views in ASP.net MVC 4
提问by Espen S.
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:
我基本上是在我的索引视图中尝试执行以下操作:
- List the current items in the database of type "Note" in the Index view (that's easy)
- Creating new items in the same Index view (not so easy).
- 在索引视图中列出数据库中“Note”类型的当前项目(这很简单)
- 在同一个索引视图中创建新项目(不是那么容易)。
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.
任何提示将非常感谢!如果您需要更多信息,请告诉我。如果需要,我很乐意压缩整个项目。
采纳答案by Richard Dalton
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
回答by harriyott
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 DbSetof Notes, where you need to pass in a single Note.
您将与传递给主视图的模型相同的模型传递给局部视图,并且它们是不同的类型。该模型是DbSet的Note,这里,你需要在一个单一的传递Note。
You can do this by adding a parameter, which I'm guessing as it's the create form would be a new Note
您可以通过添加一个参数来做到这一点,我猜这是因为它的创建表单将是一个新的Note
@Html.Partial("_CreateNote", new QuickNotes.Models.Note())

