在我的主页面(称之为 index.aspx )中,我打电话给

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

这里是 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而不是模型。

其他提示

这是未经测试的:

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

在这种情况下,您的控件视图需要特定于它的视图数据。如果您的控件想要一个名为Colors的模型上的属性,那么可能:

<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(new { Colors = ViewData.Model.Colors }));%>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top