This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Return string from HtmlHelper properties - Id, Name, DisplayName and Value #566
Comments
@Kukkimonsuta thank-you for your input. We appreciate your interest in MVC 6.0 and agree this is an issue we should address. We hope to fix the issue soon and make these helpers more generally usable. |
@dougbu please review with @GrabYourPitchforks when you are ready to PR |
👌 Have already had a conversation or two w/ him. No concerns so far... |
dougbu
added a commit
that referenced
this issue
Jul 25, 2014
…)` return `string` - fixes #566 - allows compositions such as `@Html.Label("property", Html.Id("property"))` - adjust XML comments to match - add missing XML comments to `HtmlHelperValueExtensions` - correct typos in `DisplayText()` and `ValidationMessageFor()` comments - also increase XML comment consistency for changed methods
dougbu
added a commit
that referenced
this issue
Jul 30, 2014
…)` return `string` - fixes #566 - allows compositions such as `@Html.Label("property", Html.Id("property"))` - adjust XML comments to match - add missing XML comments to `HtmlHelperValueExtensions` - correct typos in `DisplayText()` and `ValidationMessageFor()` comments - also increase XML comment consistency for changed methods
dougbu
added a commit
that referenced
this issue
Aug 1, 2014
…)` return `string` - fixes #566 and part of #847 - allows compositions such as `@Html.Label("property", Html.Id("property"))` and `@Html.Raw(Html.DisplayText("property"))` (this one is not recommended) - adjust XML comments to match - add missing XML comments to `HtmlHelperValueExtensions` - nit: `TInnerModel` -> `TModelItem` - increase XML comment consistency for changed methods - generally make wording more consistent e.g. how we use words such as "returns" - use `<see langref="string|true|false|null"/>` more
Fixed in commit 7845a8f along w/ a few other |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I'd like to reopen issue on codeplex "DisplayName / DisplayText helpers should return string" from MVC 5. It was closed because it's breaking change and it was reported after the release. There is workaround specified involving rewriting TagBuilder, however that only solves one issue (see below). Now that new major version is on the horizont I believe it's time to resolve it.
Problem
Methods on
HtmlHelper
Id
,Name
,DisplayName
andValue
encode it's return value and returnHtmlString
Detailed description
Code for reference: HtmlHelper.cs:334; HtmlHelper.cs:711; HtmlHelper.cs:841
The line I have problem with is
return new HtmlString(Encode(fullName));
. The pre-encoding is in my opinion unnecessary as the HtmlString instance will never contain html that is supposed to stay as html. Methods onHtmlHelper
Id
,Name
,DisplayName
andValue
should return unencoded string as it's job for templating engine to take care of encoding these values.In the original issue there was also
DisplayText
, however looking at the current source code it doesn't encode the data and interprets it as html in which case it's necessary to use HtmlString.Proposed fix
Change methods on
HtmlHelper
Id
,Name
,DisplayName
andValue
to returnstring
instead and let template engine / developer to take care of encoding them.Use cases
Case 1 - no way to output raw value
I think we all agree that we should stay away from inline javascript, however in the real world sometimes there is just no way around. Also there should be some kind of encoding going on anyway so that the script won't break when name contains
"
, but that's not really the point of this demo - the point is that we have no way of writing the raw value of these fields unless we decode them first.Problem: developer can't use these apis to output raw value (unless we decode it first)
Workaround: add overload to
HtmlHelper.Raw
acceptingIHtmlString
which decodes the value.Case 2 - TagBuilder will re-encode attributes
Problem:
placeholder
will be encoded twice (unless we decode it first)Workaround: allow TagBuilder to accept
IHtmlString
for attributes and not re-encode them.Case 3 - you can't modify the raw string without decoding it first
Problem: part of
id
returned byIdFor
will be encoded twice,name
will be encoded twice (unless we decode them first)Workaround: use different API, however for new users this won't be obvious and why should one reinvent the wheel when
HtmlHelper
is accessible. This applies especially to theValue
helper.The text was updated successfully, but these errors were encountered: