1

各行がフォームでラップされるように、インデックス ビューを変更しました。

インデックス.cshtml

@model IEnumerable<LevEl.Models.Page>

@foreach (var item in Model)
{
  using (Html.BeginForm("Edit", "Pages", item, FormMethod.Post, new { id = "itemForm_" + item.PageId }))
  { 
  <tr>
    <td>
      @Html.DisplayFor(modelItem => item.Title)
    </td>
    ...
    ... more fields
    ...
    <td>
      @Html.CheckBoxFor(modelItem => item.IsPublished, new { onClick = "$(itemForm_" + item.PageId + ").submit();" })
    </td>
    <td>
      @Html.ActionLink("Edit", "Edit", new { id = item.PageId }) | @Html.ActionLink("Details", "Details", new { id = item.PageId }) | @Html.ActionLink("Delete", "Delete", new { id = item.PageId })
    </td>
  </tr>
  }
}

これが私のコントローラーです:

PagesController.cs

public ActionResult Index()
{
  return View(db.Pages.Include(p => p.Language).ToList());
}

[HttpPost]
public ActionResult Edit(Page page)
{
  var method = Request.HttpMethod;

  if (ModelState.IsValid)
  {
    if (page.PageContents.GroupBy(pc => pc.Language).Any(g => g.Count() > 1))
      return HttpNotFound("A page cannot have two translations in the same language.");

    db.Entry(page).State = EntityState.Modified;
    db.SaveChanges();
    ClearMenuKeys();
    return RedirectToAction("Index");
  }
  return View(page);
}

チェック ボックスの値を切り替えてコントローラーに到達すると、pagenull になります。pageポストバック時にオブジェクト全体を渡すにはどうすればよいですか?

4

1 に答える 1

2

あなたのHTMLはページの値を保存していないようです。そのフィールド@Html.DisplayForにも設定する必要があるデータのみを表示します。@Html.HiddenFor

編集:

@Html.DisplayFor(modelItem => item.Title)
@Html.HiddenFor(modelItem => item.Title)
于 2012-11-18T10:19:36.997 に答える