Asp.net mvc “的获取错误”;传递到字典中的模型项的类型为……”;
我正在使用asp.NETMVC4和EF6制作一个网站,管理员可以在其中添加新的客户端信息。到目前为止,一切正常,但每当我试图通过按Asp.net mvc “的获取错误”;传递到字典中的模型项的类型为……”;,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我正在使用asp.NETMVC4和EF6制作一个网站,管理员可以在其中添加新的客户端信息。到目前为止,一切正常,但每当我试图通过按Add按钮保存数据时,就会出现此错误 传递到字典中的模型项的类型为“MyMvc.Models.UserInfo”,但此字典需要类型为“MyMvc.Models.BrManagement”的模型项 这是我的密码 控制器 [HttpPost] public ActionResult ClientManager(BrManagement ClTable) {
Add
按钮保存数据时,就会出现此错误
传递到字典中的模型项的类型为“MyMvc.Models.UserInfo”,但此字典需要类型为“MyMvc.Models.BrManagement”的模型项
这是我的密码
控制器
[HttpPost]
public ActionResult ClientManager(BrManagement ClTable)
{
if (Session["AdminNAME"] != null)
{
if (ModelState.IsValid)
{
var AddClient = ClTable.AddUserInfo;
abdb.UserInfoes.Add(AddClient);
abdb.SaveChanges();
return RedirectToAction("ClientManager", new { ClPanelId = "AllCl" });
}
return View(ClTable.AddUserInfo);
}
else
{
return RedirectToAction("AdminLogin");
}
}
型号
public class BrManagement
{
public Branch Branches { get; set; }
public IEnumerable<Branch> BrCollection { get; set; }
public UserInfo AddUserInfo { get; set; }
public IEnumerable<UserInfo> UserCollection { get; set; }
}
您的视图expect
br管理
@model MyMvc.Models.BrManagement
但是您返回了ClTable.AddUserInfo
,这是UserInfo
的类型,这就是为什么会出现该错误
要解决此问题,请更改
返回视图(ClTable.AddUserInfo)代码>到返回视图(ClTable)代码>在控制器中。您的返回返回视图(ClTable.AddUserInfo)
属性AddUserInfo
的类型为UserInfo
,而不是BrManagement
-将其更改为返回视图(ClTable)
您是否在视图中有@model MyMvc.Models.BrManagement
语句?@devqon,是的。我已经知道了。@StephenMuecke,现在我得到了这个错误,无法对我视图开头的空引用执行运行时绑定,第5行:string active=ViewBag.ClActive.ToString()
返回视图时,您没有设置ViewBag.clative
的值,因此它为空,因此出现错误。但是您仍然有许多其他错误,例如“@Html.HiddenFor(a=>a.AddUserInfo.IsActive,new{@Value=“N”})`现在我遇到了这个错误,无法在视图开始时对空引用执行运行时绑定,第5行:string active=ViewBag.ClActive.ToString()代码>检查您的ViewBag中是否有输入错误。确保ViewBag.ClActive存在。我不明白,我没有发现打字错误。如果我提供整个视图
代码,以便您可以更好地查看,可以吗?当然,我非常确定ViewBag.ClActive不存在,您正试图从中获取值,就像Stephen所说的那样。
@model ABCoLtd.Models.BrManagement
@{
ViewBag.Title = "ClientManager";
string active = ViewBag.ClActive.ToString();
Layout = "~/Views/Shared/_ALayout.cshtml";
}
<link href="~/Content/DataTables-1.10.4/css/jquery.dataTables.min.css" rel="stylesheet" />
<body>
<script src="~/Scripts/DataTables-1.10.4/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
var tabpane = '@active';
$("#" + tabpane).addClass("active");
$('#BrTable').DataTable({
"aoColumns": [
{ "bSortable": true },
{ "bSortable": true },
{ "bSortable": true },
{ "bSortable": true }
]
});
});
</script>
<br /><br /><br /><br />
<div class="container well" style="min-width: 100%; padding-right: 5px;">
<h3>Client Manager</h3><hr style="border-top: 2px solid #096596;" />
<ul class="nav nav-tabs">
<li><a href="#AllCl" role="tab" data-toggle="tab">All Clients</a> </li>
<li><a href="#AddCl" role="tab" data-toggle="tab">Add Clients</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="AllCl" style="padding-top: 10px; padding-left: 10px;">
<h4>Manage Clients</h4><hr style="border-top: 2px solid #096596;" />
<div class="table-responsive">
<table id="BrTable" class="table table-striped">
<thead>
<tr><th>BOID</th><th>Name</th><th>Email</th><th>Phone</th></tr>
</thead>
<tbody>
@foreach(var item in Model.UserCollection)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.UserId)</td>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Email)</td>
<td>@Html.DisplayFor(modelItem => item.Phone1)</td></tr>
}
</tbody>
</table>
</div>
</div>
<div class="tab-pane" id="AddCl" style="padding-top: 10px; padding-left: 10px;">
<h4>Add Client</h4><hr style="border-top: 2px solid #096596;" />
@using (Html.BeginForm("ClientManager", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true)
<div class="editor-label">
<strong>Client BO Account No.</strong>
</div>
<div class="editor-field">
@Html.TextBoxFor(a => a.AddUserInfo.BOAccountNo, new { size = 30 })
@Html.ValidationMessageFor(a => a.AddUserInfo.BOAccountNo)
</div>
<div class="editor-label">
<strong>Client USER ID</strong>
</div>
<div class="editor-field">
@Html.TextBoxFor(a => a.AddUserInfo.UserId)
@Html.ValidationMessageFor(a => a.AddUserInfo.UserId)
</div>
<div class="editor-label">
<strong>Client Password</strong>
</div>
<div class="editor-field">
@Html.TextBoxFor(a => a.AddUserInfo.Password)
@Html.ValidationMessageFor(a => a.AddUserInfo.Password)
</div>
<div class="editor-label">
<strong>Full Name</strong>
</div>
<div class="editor-field">
@Html.TextBoxFor(a => a.AddUserInfo.Name, new { size = 30 })
@Html.ValidationMessageFor(a => a.AddUserInfo.Name)
</div>
<div class="editor-label">
<strong>Address</strong>
</div>
<div class="editor-field">
@Html.TextBoxFor(a => a.AddUserInfo.AddressLine1, new { size = 30 })
@Html.ValidationMessageFor(a => a.AddUserInfo.AddressLine1)
</div>
@Html.HiddenFor(a => a.AddUserInfo.CreatedDate, new { @Value = System.DateTime.Now })
@Html.HiddenFor(a => a.AddUserInfo.IsActive, new { @Value = "N" })
@Html.HiddenFor(a => a.AddUserInfo.IsApproved, new { @Value = "N" })
@Html.HiddenFor(a => a.AddUserInfo.IsinfoMatched, new { @Value = "N" })
@Html.HiddenFor(a => a.AddUserInfo.IsReportView, new { @Value = "N" })
@Html.HiddenFor(a => a.AddUserInfo.IsVarified, new { @Value = "N" })
@Html.HiddenFor(a => a.AddUserInfo.UserType, new { @Value = "C" })
<br />
<p><input type="submit" class="btn btn-info" value="Add" /></p>
}
</div>
</div>
</div>
</body>
@model MyMvc.Models.BrManagement