2

私はコントローラーアクションを持っています:

 public ActionResult Sub(int id)
    {
      SubPage_Table subpage_table = db.SubPage_Table.Single(s => s.PageID == id);
        if (subpage_table == null)
        {
            return HttpNotFound();
        }
        return View(subpage_table);
    }

そしてビュー:

@model Name.Models.DB.SubPage_Table
@Html.Raw(HttpUtility.HtmlDecode(Model.PageContent))  

しかし、私のレイアウトは強く型付けする必要があります:

@model IEnumerable<Name.Models.DB.SubPage_Table> 
          @foreach (var item in Model)
          {@: <li> @Html.ActionLink((item.PageTitle), "Sub", new { id = item.PageID })</li>

}

ビューとレイアウトにデータを入力するにはどうすればよいですか?

4

1 に答える 1

2

より良いオプションは次のとおりです。

データ駆動型の情報で部分ビューを作成する

public LayoutController
{
  public ActionResult CreateMenu()
  {
    var model = db.getmenudata();

    return PartialView(model);
  }
}

次に、Layout/Createmenu.cshtml は、データ駆動型の html だけを作成します。

あなたの_Layout.cshtmlで:

<html>
... etc

@Html.RenderAction("Createmenu", "layout");

... etc

@RenderBody();


... etc
</html>
于 2014-04-29T19:17:15.470 に答える