尝试在索引页面启用弹出窗口而不是将其重定向到创建视图,ASP.NET MVC 5 & Entity Framework?

Trying to enable popup at index page instead of redirecting it to the create view, ASP.NET MVC 5 & Entity Framework?

当我在其他视图页面上创建弹出代码时,它工作得很好,它只在索引页面上抛出错误,我的模型有问题。我是 ASP.NET MVC 5 和 Entity Framework 6 的新手。非常感谢任何指导。

这是我的代码:

@model IEnumerable<LogInTest1.Models.Credentials>

我想在这里创建一个弹出窗口而不是将其重定向到创建页面。

<p>
    @Html.ActionLink("Create New", "Create")
</p>

按钮代码:

<button class="btn btn-default" onclick="AddData()">Click to Create &raquo;</button>
<script>
    function AddData() {
        $("#MyModal").modal();
    }
</script>

弹出代码:

<div class="modal fade" id="MyModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                @*@model LogInTest1.Models.Credentials*@

                @*@using (Html.BeginForm())
                {
                    @Html.AntiForgeryToken()

                    <div class="form-horizontal">
                        <h4>Credentials</h4>
                        <hr />
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(model => model.userName, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.userName, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Create" class="btn btn-default" />
                            </div>
                        </div>
                    </div>

                }*@
                @*}*@
            </div>
        </div>
    </div>
</div>

Table 索引视图中的代码:

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.userName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.password)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.userName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.password)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.empId }) |
                @Html.ActionLink("Details", "Details", new { id = item.empId }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.empId })
            </td>
        </tr>
    }
</table>

简单地说,您可以使用这样的操作 link:

<a href="" class="" data-toggle="modal" data-target="#MyModal">Create New</a>

你的索引视图有模型列表,但你的弹出窗口使用单一模型,所以你必须为你的弹出模型创建对象@{var createModel = new LogInTest1.Models.Credentials()} 然后你所有的弹出代码应该看起来像这个@Html.LabelFor(cmodel => createModel.userName, htmlAttributes: new { @class = "control-label col-md-2" })