0
この部分図は、jQueryのダイアログに表示され削除のasp.net MVCでの部分図でforeachのではnullです3 私がDeleteボタンを押すと、NullReferenceExceptionが発生します。そのモデルはNullです。モデル
どのようにすることができますか?
@using (@Html.BeginForm("Delete","Template",FormMethod.Post))
{
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = item.Id, returnUrl = Request.Url.PathAndQuery })
</td>
</tr>
}
</table>
}
コントローラー:
[HttpGet]
public ActionResult Delete()
{
string actionName = ControllerContext.RouteData.GetRequiredString("action");
if (Request.QueryString["content"] != null)
{
ViewBag.FormAction = "Json" + actionName;
var list = new List<Template> {
new Template{ Id = 1, Name = "WorkbookTest"},
new Template{ Id = 2, Name = "ClientNavigation"},
new Template{ Id = 3, Name = "Abc Rolap"},
};
return PartialView(list);
}
else
{
ViewBag.FormAction = actionName;
return View();
}
}
[HttpPost]
public JsonResult JsonDelete(int templateId, string returnUrl)
{
// do I get here no !
if (ModelState.IsValid)
{
return Json(new { success = true, redirect = returnUrl });
}
// If we got this far, something failed
return Json(new { errors = GetErrorsFromModelState() });
}
更新:どのようにあなたのActionLinkのから判断
<table>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@using (@Html.BeginForm((string)ViewBag.FormAction, "Template"))
{
@Html.Hidden("returnUrl", Request.Url.PathAndQuery);
@Html.Hidden("templateId", item.Id)
<input type='submit' value='Delete' />
}
</td>
</tr>
}
</table>
申し訳ありませんが私はポストが実行されることはありません方法JsonDeleteを削除、追加するのを忘れ。削除リンクをクリックすると、HttpGetアクションが投稿されないので、あなたが話した部分には決して行きません。 – Elisabeth
@Elisa、ありがとう、私は何かが欠落していたと思ったが、何が確かではなかった。私の更新された答えを見てください。 – McGarnagle