ASP中的模型错误.asp.net MVC 5

本文关键字:net MVC asp 错误 模型 ASP | 更新日期: 2023-09-27 18:06:29

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

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

然而,每次我点击Update时,我都面临这个错误。

传入字典的模型项类型为1 (ACIFYPJ.Models System.Collections.Generic.List。’,但是这个dictionary需要一个类型为'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("answerswer", "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>

My controller code

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);
        }

ASP中的模型错误.asp.net MVC 5

修改动作方法

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);
    }