[英]ViewData.Model in partial is null
在我的主页(称为index.aspx
)中,我打电话
<%Html.RenderPartial("_PowerSearch", ViewData.Model);%>
这是viewdata.model != null
当我到达我的局部viewdata.model != null
时:
<%=ViewData.Model%>
说viewdata.model == null
是什么赋予了?!
您是否尝试过只传递ViewData而不是ViewData.Model? 这是我在助手中使用的简化版本(从Storefront系列中无耻地被盗):
/// <summary>
/// Renders a LoggingWeb user control.
/// </summary>
/// <param name="helper">Helper to extend.</param>
/// <param name="control">Type of control.</param>
/// <param name="data">ViewData to pass in.</param>
public static void RenderLoggingControl(this System.Web.Mvc.HtmlHelper helper, LoggingControls control, object data)
{
string controlName = string.Format("{0}.ascx", control);
string controlPath = string.Format("~/Controls/{0}", controlName);
string absControlPath = VirtualPathUtility.ToAbsolute(controlPath);
if (data == null)
{
helper.RenderPartial(absControlPath, helper.ViewContext.ViewData);
}
else
{
helper.RenderPartial(absControlPath, data, helper.ViewContext.ViewData);
}
}
请注意,我传入的是当前的ViewData而不是Model。
这未经测试:
<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(ViewData.Model.Colors));%>
在这种情况下,您的控件视图需要特定的视图数据。 如果您的控件想要模型上称为“颜色”的属性,则可能:
<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(new { Colors = ViewData.Model.Colors }));%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.