ViewData.Model의 일부는 NULL입니다
-
04-07-2019 - |
문제
내 메인 페이지에서 (전화하십시오 index.aspx
) 나는 전화한다
<%Html.RenderPartial("_PowerSearch", ViewData.Model);%>
여기 viewdata.model != null
부분에 도착하면 :
<%=ViewData.Model%>
말한다 viewdata.model == null
무엇을 주?!!
해결책
ViewData.Model 대신 ViewData를 전달해 보셨습니까? 이것은 내가 헬퍼에서 사용하는 요약 버전입니다 (상점에서 뻔뻔스럽게 도난당한 시리즈) :
/// <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));%>
귀하의 제어보기는이 경우에 맞는보기 데이터를 기대하고 있습니다. 컨트롤이 색상이라는 모델에서 속성을 원한다면 아마도 다음과 같습니다.
<%=Html.RenderPartial("_ColorList.ascx", new ViewDataDictionary(new { Colors = ViewData.Model.Colors }));%>
제휴하지 않습니다 StackOverflow