您好我试图将数据库条目表单添加到显示从我的数据库中检索到的信息的页面上的模式中,我已经使用可视工作室创建了一个脚手架模板,它包含其中的所有模型信息,然后我把它放到局部视图中,通过模型显示它,但我一直收到这个错误:
System.InvalidOperationException
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[TerminalHost.Models.buildingInfo]', but this dictionary requires a model item of type 'TerminalHost.Models.buildingInfo'"
这是我的主页:
@model IEnumerable<TerminalHost.Models.buildingInfo>
@{
ViewBag.Title = "Home Page";
}
<h3>We suggest the following:</h3>
<ol class="round">
<li class="one">
<h5>Please begin creating your network structure:</h5>
<button id="modal-opener">Add a new building</button>
</li>
<li class="two">
</li>
</ol>
<div class="Container">
<div class="Box1">
@foreach (var item in Model)
{<div>
<h4 class="Heading">@item.buildingName</h4>
<h4 class="Heading">@item.buildingNumber</h4>
<p>@item.buildingDesc1</p>
<p>@item.buildingDesc2</p>
<p>@item.buildingDesc3</p>
</div>
}
</div>
....
</div>
<div id="Modal" title="Building Details">
@Html.Partial("buildingForm")
</div>
这是我的部分视图:
@model TerminalHost.Models.buildingInfo
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>buildingInfo</legend>
<div class="editor-label">
@Html.LabelFor(model => model.buildingNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.buildingNumber)
@Html.ValidationMessageFor(model => model.buildingNumber)
</div>
....
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
控制器操作:
public class HomeController : Controller
{
thDB _db = new thDB();
public ActionResult building()
{
var buildingModel = _db.buildings.ToList().FirstOrDefault();
return View(buildingModel);
}
public ActionResult buildingForm()
{
return PartialView();
}
}
答案 0 :(得分:1)
根据您的需要,您希望在主View
中显示列表,然后您需要对行动进行一些更改:
public ActionResult building()
{
var buildingModel = _db.buildings.ToList();//you have to remove the FirstOrDefault() extension method
return View(buildingModel);
}
在您看来,请替换此行:
@Html.Partial("buildingForm")
由此:
@Html.Partial("buildingForm", new buildingInfo()) //
由于您的局部视图需要一个对象,您可以从此行传递。 我希望它会有所帮助。
答案 1 :(得分:-1)
错误说明了所有问题。
当您传递TerminalHost.Models.buildingInfo
List<TerminalHost.Models.buildingInfo>
类型的模型
表示您正在返回List<TerminalHost.Models.buildingInfo>
,但在您的部分视图中,它是:
@model TerminalHost.Models.buildingInfo
将其替换为:
@model List<TerminalHost.Models.buildingInfo>
或从行动中返回单个对象。
<强>更新:强>
将Html.Partial("buildingForm")
替换为:@Html.RenderAction("buildingForm")
你的行动应该是这样的:
public ActionResult buildingForm() {
var model = new buildingInfo();
return View(model);
}
您的PartialView的名称应为 buildingForm.cshtml ,它应位于视图&gt;&gt;主页:
@model TerminalHost.Models.buildingInfo
@{
ViewBag.Title = "Create";
Layout = null;
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>buildingInfo</legend>
<div class="editor-label">
@Html.LabelFor(model => model.buildingNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.buildingNumber)
@Html.ValidationMessageFor(model => model.buildingNumber)
</div>
....
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
您的构建操作:
public ActionResult building()
{
var buildingModel = _db.buildings.ToList();
return View(buildingModel);
}