当与Razor一起使用时,我遇到了Html Helpers的困难.所述助手在MVC 2中与web表单视图引擎一起工作良好.但不是剃须刀.我在运行时得到的错误是:
Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
Source Error:
Line 1: @using Wingspan.Web.Mvc;
Line 2: @Html.IncrementalMenu(MenuBlock.Site)
Run Code Online (Sandbox Code Playgroud)
展开显示详细编译器输出显示:
d:\...\Views\Shared\MenuTop.cshtml(2,1): error CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
d:\...\Views\Shared\MenuTop.cshtml(2,7): error CS1503: Argument 1: cannot convert from 'void' to 'System.Web.WebPages.HelperResult'
Run Code Online (Sandbox Code Playgroud)
这向我表明剃刀不喜欢我的助手,IncrementalMenu,返回void(在MVC 2 Web表单引擎视图中工作正常).
我在编译时没有出现任何错误,尽管代码行(@ Html.IncrementalMenu(...))带有以下消息的红色下划线:
Cannot implicitly convert type 'void' to 'object'
Run Code Online (Sandbox Code Playgroud)
IncrementalMenu位于Wingspan.Web.Mvc命名空间中.它的签名如下:
public static void IncrementalMenu(this HtmlHelper html, MenuBlock menuBlock)
{
// Uses an HtmlTextWriter to …Run Code Online (Sandbox Code Playgroud) 更新 问题是语法问题.@awrigley显示了在Razor中编写它的正确方法.
以下作品:
@if(Model.Thing.Prop != null)
{
Html.RenderPartial("SomePartialView", Model.Thing.Prop);
}
Run Code Online (Sandbox Code Playgroud)
您需要显示作为HTML表格Foo的给定顶部1的详细信息Bar.如果为空,如何隐藏空表或显示"未找到"消息Foo?
例如.我正在接受NullReferenceException以下一行,因为Model.Thing.Prop是null;
@{Html.RenderPartial("SomePartialView", Model.Thing.Prop);}
Run Code Online (Sandbox Code Playgroud)
故意为null,我的Repository返回null而不是null Foo.但这有点撇开,即给定一个null Model.Thing.Prop,我不想打电话给Html.RenderPartial.
更新
我尝试了以下没有运气:
@if(Model.Thing.Prop != null)
{
@{Html.RenderPartial("SomePartialView", Model.Thing.Prop);}
}
Run Code Online (Sandbox Code Playgroud)
这导致Visual Studio告诉我它预期;第1行第1列也是第1行第1列;的无效表达式(我猜这是由于MVC3的预发布状态),如果我在我得到的浏览器中点击页面
CS1501:方法'Write'没有重载需要0个参数
@Html.RenderPartial突出显示该行.
我也试过了
@if(Model.Thing.Prop != null)
{
<text>
@{Html.RenderPartial("SomePartialView", Model.Thing.Prop);}
</text>
}
Run Code Online (Sandbox Code Playgroud)
但这会导致NullReferenceException我的部分视图中出现一个似乎不对的内容.Model.Thing绝对是有效的Bar,Model.Thing.Prop绝对是一个null Foo.
asp.net-mvc renderpartial nullreferenceexception razor asp.net-mvc-3
我刚刚安装了ASP.NET MVC 3 RC来尝试升级MVC 2站点.我遇到了一个渲染问题,我设法在网站之外重新编写,使用从头创建的MVC 3项目.
这是我的Razor cshtml视图:
@using Mvc3RCTest.Helpers
<h2>Demo Render Bug</h2>
<div class="content">
@{ Html.RenderTest(); }
</div>
Run Code Online (Sandbox Code Playgroud)
RenderTest是一个HTML扩展,定义如下:
using System.Web;
using System.Web.Mvc;
namespace Mvc3RCTest.Helpers
{
public static class TestHtmlExtensions
{
public static void RenderTest(this HtmlHelper html)
{
HttpResponseBase r = html.ViewContext.HttpContext.Response;
r.Write("<ul>");
for (int i = 0; i < 10; ++i)
{
r.Write("<li>" + i + "</li>");
}
r.Write("</ul>");
}
}
}
Run Code Online (Sandbox Code Playgroud)
呈现时,HTML如下所示:
<ul><li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
<h2>Demo Render Bug</h2>
<div class="content">
</div>
Run Code Online (Sandbox Code Playgroud)
如您所见,RenderTest HTML扩展的输出在Razor模板的其余部分之前被错误地发出.好像Razor渲染引擎试图缓存整个输出,而不知道HTML扩展可以直接写入输出.
有没有人见过这个问题?任何人都知道如何解决这个问题,而不必重做我的所有HTML扩展而不是直接写入输出?