所以我想呈现的局部视图进入我的页面,我得到了以下错误:问题有关模型错误被传递到局部视图
错误
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[GettingOrganized.Models.Todo]', but this
dictionary requires a model item of type 'GettingOrganized.Models.Todo'.
我不看看局部视图或控制器出了什么问题。
局部视图
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<GettingOrganized.Models.Todo>" %>
<% using (Html.BeginForm("Create", "Todo", FormMethod.Post, new {id="CreateTodo"})) {%>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.Title) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Title) %>
<%= Html.ValidationMessageFor(model => model.Title) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
控制器索引视图呈现局部视图:
<% Html.RenderPartial("CreateElements"); %>
有什么想法?我想保持它接近这个设置,因为它是强类型的。
UPDATE
因此提供了一些细节,现在这个问题正变得越来越清晰。我在div上的页面上呈现视图并隐藏它,用户单击某个链接。然后我想要显示div。在“创建”视图中使用此相同的部分,您可以在其中创建“待办事项”。但我现在想在索引视图中使用部分,它显示了模型“Todo”的列表。
传递的模式,在“索引”的观点:
Inherits="System.Web.Mvc.ViewPage<IEnumerable<GettingOrganized.Models.Todo>>" %>
所以,如果我不想遍历foreach循环,而只是想表明该模型的一个实例,谁做我这样做?
而且我可以使用下面的视图部分,它会工作,其带走的强类型的模型:
WORKING局部
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% using (Html.BeginForm("Create", "Todo",
FormMethod.Post, new { id="CreateTodo"})) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Title">Title:</label>
<%=Html.TextBox("Title")%>
<%=Html.ValidationMessage("Title", "*")%>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<p>
<input type="submit" value="Create" />
</p>
<% } %>
可能的答案
但是,我相信我可能找到了答案。
<% Html.RenderPartial("CreateElements", new Todo()); %>
这是处理这个问题的正确方法吗?
什么是索引视图模型? – BlackICE 2011-03-24 22:35:53
我更新 - 根据“更新”与模型和更多信息 – pghtech 2011-03-25 16:29:47