c# – 将null子对象从父对象传递到局部视图
发布时间:2020-12-15 08:45:52 所属栏目:百科 来源:网络整理
导读:我有一个对象,其中包含我的ASP.NET MVC Web应用程序的模型.传递到视图中的模型具有该特定视图上“小工具”的子模型.这些子模型中的每一个都被传递到局部视图(小工具). 问题是我在视图模型中有一个空模型.见下面的例子. 查看型号: public class FooBarHolder
我有一个对象,其中包含我的ASP.NET MVC Web应用程序的模型.传递到视图中的模型具有该特定视图上“小工具”的子模型.这些子模型中的每一个都被传递到局部视图(小工具).
问题是我在视图模型中有一个空模型.见下面的例子. 查看型号: public class FooBarHolder() { public FooBar1 FooBar1 { get; set; } public FooBar2 FooBar2 { get; set; } } 我们将FooBarHolder传递到视图中,并在视图内部进行调用,例如 <% Html.RenderPartial("Foo",Model.FooBar1); %> <% Html.RenderPartial("Foo2",Model.FooBar2); %> 现在说比较Model.FooBar2为null.我从强类型的局部视图中遇到的是一个错误,上面写着“这个视图需要一个类型为FooBar2的模型,但却得到了一个类型为FooBarHolder的模型.” 为什么会发生这种情况而不是只传入null? 解决方法
这就是
RenderPartial方法的工作方式(我知道应该有记录,博客等等……我也觉得这有点奇怪).如果未指定模型或传递null,则将使用父页面的模型.为避免这种情况,您可以使用null合并运算符:
<% Html.RenderPartial("Foo",Model.FooBar1 ?? new Foo()); %> 如果您对如何实现它非常好奇,那么可以从ASP.NET MVC 2源代码的相关部分中摘录: // Renders the partial view with an empty view data and the given model public static void RenderPartial(this HtmlHelper htmlHelper,string partialViewName,object model) { htmlHelper.RenderPartialInternal(partialViewName,htmlHelper.ViewData,model,htmlHelper.ViewContext.Writer,ViewEngines.Engines); } internal virtual void RenderPartialInternal(string partialViewName,ViewDataDictionary viewData,object model,TextWriter writer,ViewEngineCollection viewEngineCollection) { if (String.IsNullOrEmpty(partialViewName)) { throw new ArgumentException(MvcResources.Common_NullOrEmpty,"partialViewName"); } ViewDataDictionary newViewData = null; if (model == null) { if (viewData == null) { newViewData = new ViewDataDictionary(ViewData); } else { newViewData = new ViewDataDictionary(viewData); } } else { if (viewData == null) { newViewData = new ViewDataDictionary(model); } else { newViewData = new ViewDataDictionary(viewData) { Model = model }; } } ViewContext newViewContext = new ViewContext(ViewContext,ViewContext.View,newViewData,ViewContext.TempData,writer); IView view = FindPartialView(newViewContext,partialViewName,viewEngineCollection); view.Render(newViewContext,writer); } 注意如何处理null模型的情况. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |