partialのViewData.Modelはnullです
-
04-07-2019 - |
質問
メインページ( index.aspx
と呼びます)で呼び出します
<%Html.RenderPartial("_PowerSearch", ViewData.Model);%>
ここで viewdata.model!= null
パーシャルに到着したとき:
<%=ViewData.Model%>
viewdata.model == null
何が得られますか?
解決
ViewData.Modelの代わりにViewDataを渡してみましたか?これは、ヘルパーで使用している短縮版です(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 }));%>
所属していません StackOverflow