HTMLHelper不仅可以用于属性值,还可以用于属性名
本文关键字:属性 用于 还可以 HTMLHelper | 更新日期: 2023-09-27 18:01:30
我想根据我传递的特定属性的值来生成。下面是我想使用的帮助器:
<sometag @PossibleHelper(parameter)/>
在PossibleHelper做了他的事情之后,结果可能是:
<sometag attributeName="attributeValue"/>
如何在帮助程序中表达呢?
@helper PossibleHelper(someType){
if(condition){
attributeName="attributeValue" //this is wrong
}
}
当你在一个helper中,它只是普通的razor语法。
@helper PossibleHelper(SomeType something) {
if (condition) {
<span attributeName="attributeValue">blah</span>
}
}
你可以这样设置一个属性。
@helper PossibleHelper(int something) {
var attrValue = string.Empty;
if (true) {
attrValue = "attributeValue";
}
@(string.Format("attribute={0}", attrValue))
}
用法:
<sometag @PossibleHelper(parameter)/>
供参考,你也可以为HtmlHelper
做一个扩展方法,如果代码在视图之间共享或有相当数量的逻辑,这可能会更好。
public static class HtmlHelperExtensions
{
public static MvcHtmlString SomeExtension(this HtmlHelper htmlHelper)
{
// ...
}
}