簡體   English   中英

ViewData.Model中的null為null

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM