简体   繁体   English

ViewData.Model中的null为null

[英]ViewData.Model in partial is null

In my main page (call it index.aspx ) I call 在我的主页(称为index.aspx )中,我打电话

<%Html.RenderPartial("_PowerSearch", ViewData.Model);%>

Here the viewdata.model != null When I arrive at my partial: 这是viewdata.model != null当我到达我的局部viewdata.model != null时:

<%=ViewData.Model%>

Says viewdata.model == null viewdata.model == null

What gives?! 是什么赋予了?!

Have you tried just passing in ViewData instead of ViewData.Model? 您是否尝试过只传递ViewData而不是ViewData.Model? This is an abridged version what I use in my helpers (shamelessly stolen from the Storefront series): 这是我在助手中使用的简化版本(从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);
        }
    }

Note that I pass in the current ViewData and not the Model. 请注意,我传入的是当前的ViewData而不是Model。

This is untested: 这未经测试:

<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(ViewData.Model.Colors));%>

Your control view is expecting view data specific to it in this case. 在这种情况下,您的控件视图需要特定的视图数据。 If your control wants a property on the model called Colors then perhaps: 如果您的控件想要模型上称为“颜色”的属性,则可能:

<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(new { Colors = ViewData.Model.Colors }));%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM