【发布时间】:2012-11-08 07:13:01
【问题描述】:
我写了一个这样的部分方法:
public ActionResult _StatePartial()
{
ViewBag.MyData = new string[] { "110", "24500" };
return View();
}
并在_Layout页面中渲染_StatePartial视图:
@Html.Partial("_StatePartial")
这是我的部分视图代码:
@{
string[] Data = (string[])ViewBag.MyData;
}
<div id="UserState">
Reputation: @Html.ActionLink(Data[0], "Reputation", "Profile") |
Cash: @Html.ActionLink(Data[1], "Index", "FinancialAccount")
</div>
但是当我运行这个项目时,_StatePartial 方法不会被调用并且ViewBag 总是为空。
Server Error in '/' Application.
Object reference not set to an instance of an object.
请注意,这些参数不是我的模型字段,而是通过调用 Web 服务方法来计算的。但我一直在我的问题中设置这些值。
我可以为此做些什么? 将参数传递给局部视图的任何想法? 谢谢。
【问题讨论】:
标签: c# asp.net asp.net-mvc-4 partial-views