ASP.NET MVC 5 中的模型错误

Model error in ASP.NET MVC 5

我目前正在尝试对 table 的特定行进行更新。例如,在一个页面上,我检索了所有数据并将其显示在 WebGrid 上。

WebGrid 中,我添加了一个带有 button 的列,名为 Update。用户将单击 Update 按钮并被重定向到另一个页面以更新详细信息。

但是,我每次点击 Update 时都会遇到这个错误。

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ACIFYPJ.Models.Response]', but this dictionary requires a model item of type 'ACIFYPJ.Models.Response'.

这些是我的代码。

包含显示所有数据的 Webgrid 的页面

@model List<ACIFYPJ.Models.Response>

@{
    ViewBag.Title = "List Of Responses";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5);
    grid.Pager(WebGridPagerModes.All);
}

<h2 style="margin-bottom: 3%; margin-top: 3%;">List Of Responses</h2>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>

<script>
    $(document).ready(function () {
        $("#responseFeedBack").click(function () {

            var tr = $(this).parents('tr:first');
            var responseID = tr.find("#responseID").text();
            alert(responseID);
            var ResponseModel = {
                "responseID": responseID
            };

            $.ajax({

                url: '@Url.Action("ResponseFeedBack", "Course")',
                type: 'POST',
                data: {
                    "responseID": responseID
                },
                async: false,
                dataType: 'text',
                success: function (data) {

                }
            });
        });
    });

</script>

<div id="content">
    @grid.GetHtml(
    fillEmptyRows: false,
    tableStyle: "webgrid-table",
    headerStyle: "webgrid-header",
    footerStyle: "webgrid-footer",
    alternatingRowStyle: "webgrid-alternating-row",
    selectedRowStyle: "webgrid-selected-row",
    rowStyle: "webgrid-row-style",
    mode: WebGridPagerModes.All,
    firstText: "<< First",
    previousText: "<Prev",
    nextText: "Next >",
    lastText: "Last >>",

    columns: grid.Columns(
        grid.Column("responseID", "Response ID", format: @<text><span class="display-mode" id="responseID">@item.responseID</span></text>),

        grid.Column("name", "Sender's Name", format: @<text><span id="senderName" class="display-mode">@item.Name</span></text>),

        grid.Column("message", "Message", format: @<text><span class="display-mode"><label id="lblMessage">@item.message</label></span>
        </text>),

        grid.Column("status", "Message Status", format: @<text><span class="display-mode"><label id="lblStatus">@item.status</label></span>
        </text>),

        grid.Column("answer", "Answer", format: @<text><span class="display-mode"><label id="lblAnswer">@item.answer</label></span>
        </text>),

        grid.Column("", format: @<text>
            <button id="responseFeedBack">Reply</button>
        </text>, style: "col3Width", canSort: false)
))
</div>

<div style="margin-top: 2%;">
    @Html.ActionLink("Back to Home", "AdminIndex", "Home")
</div>

点击 WebGrid

上的 Update 按钮后,用户将被重定向到此页面
    @model ACIFYPJ.Models.Response

@{
    ViewBag.Title = "Send your Response";
}

<h2>Feedback Form</h2>

@using (Html.BeginForm("ResponseFeedBack", "Course", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{
    @Html.AntiForgeryToken()

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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.answer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.answer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.answer, "", 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>
    @Html.ActionLink("Back to Home", "Index")
</div>

我的控制器代码

public ActionResult ViewAllResponse()
        {
            var responses = new List<Response>();
            using (ACIFYPJEntities aci = new ACIFYPJEntities())
            {
                responses = aci.Responses.ToList();
            }
            return View(responses);
        }


        public ActionResult ResponseFeedBack(int responseID)
        {
            var responses = new List<Response>();

            using (ACIFYPJEntities aci = new ACIFYPJEntities())
            {
                responses = (from c in aci.Responses
                             where c.responseID == responseID
                             select c).ToList();
            }

            return View(responses);
        }

改变你的行动方式

public ActionResult ResponseFeedBack(int responseID)
    {
        var responses = Response();

        using (ACIFYPJEntities aci = new ACIFYPJEntities())
        {
            responses = (from c in aci.Responses
                         where c.responseID == responseID
                         select c).First();
        }

        return View(responses);
    }