現在、テーブルの特定の行に対して更新を実行しようとしています。たとえば、あるページですべてのデータを取り出してWebGrid
に表示します。ASP.NET MVC 5のモデルエラー
WebGrid
には、Update
と呼ばれるbutton
の列を追加しました。ユーザーはUpdate
ボタンをクリックし、別のページにリダイレクトされて詳細を更新します。
ただし、私はUpdate
をクリックするたびにこのエラーに直面しています。
辞書に渡さモデル項目タイプ 「System.Collections.Generic.List`1 [ACIFYPJ.Models.Response]」であるが、これ 辞書はタイプ「ACIFYPJ.Modelsのモデル項目が必要。応答'。
これは私のコードです。
私のコントローラのコードWebGrid
@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>
にUpdate
ボタンをクリックした後
@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を含むページ
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);
}
返されたビュー(応答)のため、更新をクリックした後に正確に表示されるはずのもの。あなたがしているメソッドを返すビュー(応答);これはResponse型のリストですが、ビューには@model ACIFYPJ.Models.Responseがあります。リダイレクトされたビューで正確に必要なものに基づいてそれらの1つを変更する必要があります。 –
最初のビュー( 'ViewAllResponse')には、webgridが表示されます。 webgridには、 'Update'ボタンがあります。 'Update'ボタンをクリックすると、ユーザーは' ResponseFeedBack'というビューにリダイレクトされます。このビューは、クリックされた「行」ユーザーに基づいて情報を表示します。 @ RaviA。 @RaviA。 – Alvin
長い間、この問題を解決するように見えることはできませんでした。助けてください、ありがとう! – Alvin