2013-02-27 110 views
2

我想根据我传递一个特定的属性与他的价值产生。这是我要如何使用助手:可以将HTMLHelper不仅用于属性值,而是用于属性的名称?

<sometag @PossibleHelper(parameter)/> 

PossibleHelper后做他的事,这可能是结果:

<sometag attributeName="attributeValue"/> 

我怎样才能表达,在助手?

@helper PossibleHelper(someType){ 

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

回答

2

当你在助手中时,它只是普通的剃刀语法。

@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) 
    { 
     // ... 
    } 
} 
+0

谢谢Tyriar。我会补充(在阅读和测试一些后)@函数也可以做我想问的东西。 – mjsr 2013-03-21 21:10:26

相关问题