Spark View Engine Bindings with Class Html Attribute

帅比萌擦擦* 提交于 2019-12-07 23:13:45

问题


I've been using the Bindings feature with the spark view engine to replace quite a few of my Html helper method calls.

One problem I have is that I want to define a css class to pass in to the anonymous dictionary parameter of the helper methods, which I would normally do like this:

${Html.EditorFor(x=>x.Username, new{@class = "css-class"})}

Replacing this with a spark Binding looks like this:

Binding:

<element name="Editor">Html.EditorFor(x => x.@For, new {"@*"}) </element>

View Element:

<Editor For="Password" class="css-class" />

I get an error "A namespace cannot directly contain members such as fields or method" which is valid because it is resolving to

Output.Write(Html.EditorFor(x => x.Password, new {class="big"}) );

The issue is obviously that I need to use @class instead of class.

However, I can't specify @class in the html attribute like this

<Editor For="Password" @class="css-class" />

Because spark ignores it.

I also can't specify it like this:

<element name="Editor">Html.EditorFor(x => x.@For, new {@"@*"}) </element>

Because then every html attribute that gets passed through will be prefixed with the @ symbol.

So my question is, how can I pass the class HTML attribute through to the spark binding when using it to initialize a dictionary parameter so that it doesn't throw a compiler error?


回答1:


You're correct in your observations. The correct syntax in your binding I believe would be something like:

<element name="Editor">Html.EditorFor(x => x.@For, new Dictionary[[string,object]]{{"@*"}}) </element>

instead of

<element name="Editor">Html.EditorFor(x => x.@For, new {"@*"}) </element>

That way, any attributes you attach to your tag in the view that are not specifically taken care of by name, will pass through as name value pair attributes on the rendered output.

I hope I got the syntax right, does that work for you?

All the best, Rob




回答2:


Sorry to hijack this question. But for anyone starting with bindings, if you are using the stable Spark 1.1.0, I think there are a couple bugs regarding bindings with # and child::*. The fix being to use the development spark build.

If this is not the case, hopefully Robert will correct me. But if it is, then perhaps this will allow beginners (like myself) to avoid some initial confusion.



来源:https://stackoverflow.com/questions/4154999/spark-view-engine-bindings-with-class-html-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!