30-局部视图的数据传递

局部视图可以访问父视图中的ViewData的数据

但是它们访问的是ViewData的一个副本。

所以在局部视图中修改ViewData是不会影响到父视图的。

我们前面说的方法 都还可以带上一个数据参数过去的。

然后就在布局视图这边通过@model来获取数据

@{
    ViewData["Title"] = "在布局视图中修改了Title";
}

<div style="background-color:red;height:250px;width:250px;">
    右侧栏的内容 @ViewData["Title"]
</div>
<br />Code language: HTML, XML (xml)

通过第二个参数传递数据

@Html.Partial("_Right","www.FoxDevelop.COM")Code language: JavaScript (javascript)

然后在局部视图这边

@model string

@{
    ViewData["Title"] = "在布局视图中修改了Title";
}

<div style="background-color:red;height:250px;width:250px;">
    右侧栏的内容 @ViewData["Title"]
</div>
<h1>@Model</h1>
<br />Code language: HTML, XML (xml)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注