-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Is @Html.Raw(...) working? ... or am I just a nOOb? #2392
Comments
should work fine. suspect the issue is the second |
Thanks. I changed it, but I found out that ...
works, but
throws the The string for that is simply in the top of the rendered body view:
Clearly, I'm learning MVC and not doing this right; however, |
Ok, so I get it now that
in the shared view with
works. It still seems strange that I can't just inject the entire HTML tag into the view. I'll leave this open while my concern about that |
Does casting Something like: @Html.Raw(ViewBag.Description as string)) |
This is likely a bug:
|
This does work as expected with MVC 5. |
interesting. there is absolutely nothing special about the also, which MVC and DNX versions are you testing? |
I was using CoreCLR: dnx-coreclr-x64-1.0.0-beta5-11556 (and MVC 6) |
@dougbu I'll be back in the office in three hours (1pm CST). If nobody else checks DNX variants by then, I'll do it and report back. |
Html.Raw used to be an instance method but is now an extension method. Extension methods unfortunately cannot be called with This is technically by design but we could consider a different approach here perhaps, such as getting rid of |
@Eilon |
The original exception in my app was When I went back and tested in my HelloMVC app, which in my version includes a Sort of made me think that ... anyway, check this out ... When I try this Does this mean that If an object is provided to |
@dougbu maybe this is because most cases you call raw with string and you avoid the null check ? I also don't think it's necessary. I still don't know why the object one is throwing, this worked with dynamics in mvc5 with no problem. |
@dougbu oh yeah weird I guess it's the same as it always was... so not sure what's going on here. |
@dougbu @Eilon Repro: public interface IMyHtmlHelper
{
HtmlString Raw(object x);
}
public interface IMyHtmlHelper<T> : IMyHtmlHelper { }
public class MyHtmlHelper : IMyHtmlHelper
{
public HtmlString Raw(object value)
{
return new HtmlString(value == null ? null : value.ToString());
}
}
public class MyHtmlHelper<T> : MyHtmlHelper, IMyHtmlHelper<T> { } With this view: @{
IMyHtmlHelper<object> MyHtml = new MyHtmlHelper<object>();
ViewBag.Test = "<strong>Hello World!</strong>";
}
<p>
@MyHtml.Raw(ViewBag.Test)
</p> Causes the same
However, when I change this line: IMyHtmlHelper<object> MyHtml = new MyHtmlHelper<object>(); to this: MyHtmlHelper<object> MyHtml = new MyHtmlHelper<object>(); (notice that the type of It does work. Using the non-generic type works too: IMyHtmlHelper MyHtml = new MyHtmlHelper(); Should this be a new issue? |
@guardrex the behaviour seen here is as-expected when attempting to pass a You have a number of potential workarounds here, including the cast @henkmollema first mentioned. For example:
Since BTW @Bartmax the specific casts, |
@dougbu so there is a way to fix this and keep both advantages. By just changing |
@yishaigalatzer that approach might work in some cases but it isn't possible here. |
Clearing milestone and assignee in case triage wants to take another look at this. |
👍 just ran into this myself. using |
Possible options include:
|
A couple of new options in addition to avoiding or removing
tl;dr; class Program
{
public static void Main(string[] args)
{
dynamic joe = "fred";
IMyHtmlHelper2 myHtml = new MyHtmlHelper2();
Console.WriteLine(myHtml.Raw(joe));
}
private interface IMyHtmlHelper
{
string Raw(object value);
string Raw(string value);
}
private interface IMyHtmlHelper2 : IMyHtmlHelper
{
}
private class MyHtmlHelper2 : IMyHtmlHelper2
{
public string Raw(object value)
{
return value == null ? null : value.ToString();
}
public string Raw(string value)
{
return value;
}
}
} Note the problems occur after eliminating generics from the scenario and after removing inheritance from the implementing class. I've also run the above in .NET 4.5 and encountered the identical |
Looks like this has been a bug for a while: |
Removed Design label and marked this as a Bug (because it's a regression compared to MVC 5). |
- #2392 - `dynamic` does not work correctly when inherited from a base `interface`
- #2392 - `dynamic` does not work correctly when inherited from a base `interface`
Trying to use
@Html.Raw(@ViewBag.xxx)
in a view to inject some raw HTML into the view, but I'm receiving:RuntimeBinderException: 'Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<object>' does not contain a definition for 'Raw'
I'm new to MVC, so I'm not surprised if I'm just doing something wrong.
The text was updated successfully, but these errors were encountered: