获取“传入字典的模型项的类型......”的错误

时间:2015-03-16 09:24:26

标签: asp.net-mvc entity-framework

我正在使用asp.net mvc 4&管理员可以添加新客户端信息的EF6。到目前为止一切正常,但每当我尝试按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; }
}

查看

@using (Html.BeginForm("ClientManager", "Home", FormMethod.Post))
            {
                @Html.ValidationSummary(true)
                <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" })
                <br />
                <p><input type="submit" class="btn btn-info" value="Add" /></p>
            }

我为Branches&amp;使用了相同的代码BrCollection模型,它们都运行良好。 UserCollection模型也运行正常。为什么AddUserInfo模型会发生这种情况?我搜索了很多,但找不到任何类似于我的解决方案。需要这个帮助真的很糟糕。我们将不胜感激! TNX。

更新 查看(FULL)

@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>

1 个答案:

答案 0 :(得分:0)

您的观看期待BrManagement

@model MyMvc.Models.BrManagement

但是您返回的ClTable.AddUserInfo类型为UserInfo,这就是您收到错误的原因。

要解决此问题,请在控制器中将return View(ClTable.AddUserInfo);更改为return View(ClTable);