Esp*_* S. 77 c# asp.net-mvc partial-views razor asp.net-mvc-4
我最近开始玩ASP.net MVC(4),但我无法解决我遇到的这个问题.我相信你知道它很容易.
我基本上试图在我的索引视图中执行以下操作:
所以我想我需要一个局部视图,并且我创建了如下(_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>
}
Run Code Online (Sandbox Code Playgroud)
在我原来的索引视图(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>
Run Code Online (Sandbox Code Playgroud)
(使用:@ 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
Run Code Online (Sandbox Code Playgroud)
我的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");
}
Run Code Online (Sandbox Code Playgroud)
我认为这与事实,索引视图使用不同的模型,如@model的IEnumerable做,但无论怎样我改变它周围,使用的RenderPartial,的RenderAction,改变的ActionResult到的ViewResult等,我不能让它工作.
任何提示将非常感谢!如果您需要更多信息,请与我们联系.如果需要,我很乐意压缩整个项目.
Ric*_*ton 128
更改加载局部视图的代码:
@Html.Partial("_CreateNote", new QuickNotes.Models.Note())
Run Code Online (Sandbox Code Playgroud)
这是因为部分视图期望一个Note但是正在传递父视图的模型,即IEnumerable
har*_*ott 38
您将相同的模型传递给局部视图,因为传递给主视图,它们是不同的类型.该模型是DbSet的Note,这里,你需要在一个单一的传递Note.
你可以通过添加一个参数来做到这一点,我猜测它是创建形式的新参数 Note
@Html.Partial("_CreateNote", new QuickNotes.Models.Note())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
228327 次 |
| 最近记录: |