如何在 mvc3 EF5 中的 _layout.cshtml 侧边栏内绑定模态数据
How to bind modal data inside sidebar in _layout.cshtml in mvc3 EF5
我想在 _layout.cshtml 页面的右侧边栏中显示模态数据,因此数据将显示在每个视图上。在这里,我为放置在 _layout 中右侧栏中的数据创建了一个 PartialView。它在索引页面上工作正常,但是当我重定向到另一个页面时,它给了我错误。
_layout.cshtml
<div class="col-md-9 contentbar">
@RenderBody()
</div>
<div class="col-md-3" style="padding-top: 20px;">
<ul style="font-weight: bold; list-style-type: none; padding: 0px;">
<li style="font-weight: bold;">Tv Serial Latest
@{Html.RenderPartial("ListAllTvt");}
</li>
</ul>
</div>
</div>
部分视图 (ListAllTvt.cshtml)
@model IEnumerable<Cmedia.Models.TVSerialModel>
<ul style="padding: 0; font-weight: normal; list-style-type: none;">
@foreach (var item in Model)
{
<li>@Html.ActionLink(@item.Serial_Name, "ListAllEpisode", "TvSerialEpisode",new { id = item.Serial_ID}, null) </li>
}
</ul>
TvSerialContoller.cs
public class TvSerialController : Controller
{
public ViewResult Index()
{
List<TVSerialModel> tv = tvContext.dbTvSerials.ToList();
return View(tv);
}
public ActionResult Detail(Int32 id)
{
TVSerialModel tvserial = tvContext.dbTvSerials.Find(id);
return View(tvserial);
}
public ActionResult ListAllTvSeri()
{
List<TVSerialModel> tv = tvContext.dbTvSerials.ToList();
return View(tv);
}
}
我猜当调用索引视图时它默认设置模型值,因为索引和部分视图使用相同的模型,但是当我重定向到另一个页面时它刷新模型数据然后我得到错误。我不知道如何解决这个问题。
如果您需要与布局中的数据进行交互,则必须使用子操作。其他都行不通。
public class TvSerialController : Controller
{
...
[ChildActionOnly]
public ActionResult ListAllTvt()
{
var model = // get data;
return PartialView("_ListAllTvt", model);
}
}
在您的布局中:
@Html.Action("ListAllTvt", "TvSerial")
我想在 _layout.cshtml 页面的右侧边栏中显示模态数据,因此数据将显示在每个视图上。在这里,我为放置在 _layout 中右侧栏中的数据创建了一个 PartialView。它在索引页面上工作正常,但是当我重定向到另一个页面时,它给了我错误。
_layout.cshtml
<div class="col-md-9 contentbar">
@RenderBody()
</div>
<div class="col-md-3" style="padding-top: 20px;">
<ul style="font-weight: bold; list-style-type: none; padding: 0px;">
<li style="font-weight: bold;">Tv Serial Latest
@{Html.RenderPartial("ListAllTvt");}
</li>
</ul>
</div>
</div>
部分视图 (ListAllTvt.cshtml)
@model IEnumerable<Cmedia.Models.TVSerialModel>
<ul style="padding: 0; font-weight: normal; list-style-type: none;">
@foreach (var item in Model)
{
<li>@Html.ActionLink(@item.Serial_Name, "ListAllEpisode", "TvSerialEpisode",new { id = item.Serial_ID}, null) </li>
}
</ul>
TvSerialContoller.cs
public class TvSerialController : Controller
{
public ViewResult Index()
{
List<TVSerialModel> tv = tvContext.dbTvSerials.ToList();
return View(tv);
}
public ActionResult Detail(Int32 id)
{
TVSerialModel tvserial = tvContext.dbTvSerials.Find(id);
return View(tvserial);
}
public ActionResult ListAllTvSeri()
{
List<TVSerialModel> tv = tvContext.dbTvSerials.ToList();
return View(tv);
}
}
我猜当调用索引视图时它默认设置模型值,因为索引和部分视图使用相同的模型,但是当我重定向到另一个页面时它刷新模型数据然后我得到错误。我不知道如何解决这个问题。
如果您需要与布局中的数据进行交互,则必须使用子操作。其他都行不通。
public class TvSerialController : Controller
{
...
[ChildActionOnly]
public ActionResult ListAllTvt()
{
var model = // get data;
return PartialView("_ListAllTvt", model);
}
}
在您的布局中:
@Html.Action("ListAllTvt", "TvSerial")