gpt4 book ai didi

c# - HTMLHelper 不仅可以用于属性值,还可以用于属性名称吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:58 25 4
gpt4 key购买 nike

我想根据我传递的特定属性及其值来生成。这就是我想要使用助手的方式:

<sometag @PossibleHelper(parameter)/>

在 PossibleHelper 完成他的工作后,这可能是结果:

<sometag attributeName="attributeValue"/>

我如何在助手中表达它?

@helper PossibleHelper(someType){

if(condition){
attributeName="attributeValue" //this is wrong
}
}

最佳答案

当你在帮助程序中时,它只是常规的 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 创建一个扩展方法,如果代码在 View 之间共享或者有相当多的逻辑,这可能会更好。

public static class HtmlHelperExtensions
{
public static MvcHtmlString SomeExtension(this HtmlHelper htmlHelper)
{
// ...
}
}

关于c# - HTMLHelper 不仅可以用于属性值,还可以用于属性名称吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com