Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ViewData.partial中的模型为空_C#_Asp.net Mvc_Model - Fatal编程技术网

C# ViewData.partial中的模型为空

C# ViewData.partial中的模型为空,c#,asp.net-mvc,model,C#,Asp.net Mvc,Model,在我的主页(称之为index.aspx)中,我调用 表示viewdata.model==null 什么给了我 您是否尝试只传入ViewData而不是ViewData.Model?这是我在我的助手(无耻地从店面系列中偷来)中使用的一个节略版本: // ///呈现LoggingWeb用户控件。 /// ///要扩展的帮助程序。 ///控制类型。 ///查看要传入的数据。 公共静态void RenderLoggingControl(this System.Web.Mvc.HtmlHelper助手,L

在我的主页(称之为
index.aspx
)中,我调用

表示
viewdata.model==null


什么给了我

您是否尝试只传入ViewData而不是ViewData.Model?这是我在我的助手(无耻地从店面系列中偷来)中使用的一个节略版本:

//
///呈现LoggingWeb用户控件。
/// 
///要扩展的帮助程序。
///控制类型。
///查看要传入的数据。
公共静态void RenderLoggingControl(this System.Web.Mvc.HtmlHelper助手,LoggingControls控件,对象数据)
{
string controlName=string.Format(“{0}.ascx”,控件);
string controlPath=string.Format(“~/Controls/{0}”,controlName);
字符串absControlPath=VirtualPath.ToAbsolute(控制路径);
如果(数据==null)
{
RenderPartial(absControlPath,helper.ViewContext.ViewData);
}
其他的
{
RenderPartial(absControlPath,data,helper.ViewContext.ViewData);
}
}
请注意,我传入的是当前视图数据,而不是模型。

这是未测试的:

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

在这种情况下,控件视图需要特定于它的视图数据。如果控件希望模型上有一个名为“颜色”的属性,则可能:

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

谢谢,这就成功了。我向beta1版本转换的另一个微妙举动。
    /// <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);
        }
    }
<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(ViewData.Model.Colors));%>
<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(new { Colors = ViewData.Model.Colors }));%>