mvc - How can I add a class attribute to an HTML element generated by MVC's HTML Helpers?

ID : 20437

viewed : 41

Tags :

Top 5 Answer for mvc - How can I add a class attribute to an HTML element generated by MVC's HTML Helpers?

vote vote


In order to create an anonymous type (or any type) with a property that has a reserved keyword as its name in C#, you can prepend the property name with an at sign, @:

Html.BeginForm("Foo", "Bar", FormMethod.Post, new { @class = "myclass"}) 

For VB.NET this syntax would be accomplished using the dot, ., which in that language is default syntax for all anonymous types:

Html.BeginForm("Foo", "Bar", FormMethod.Post, new with { .class = "myclass" }) 
vote vote


Current best practice in CSS development is to create more general selectors with modifiers that can be applied as widely as possible throughout the web site. I would try to avoid defining separate styles for individual page elements.

If the purpose of the CSS class on the <form/> element is to control the style of elements within the form, you could add the class attribute the existing <fieldset/> element which encapsulates any form by default in web pages generated by ASP.NET MVC. A CSS class on the form is rarely necessary.

vote vote


vote vote


Many of the solutions here sort or reverse the IntStream, but that unnecessarily requires intermediate storage. Stuart Marks's solution is the way to go:

static IntStream revRange(int from, int to) {     return IntStream.range(from, to).map(i -> to - i + from - 1); } 

It correctly handles overflow as well, passing this test:

@Test public void testRevRange() {     assertArrayEquals(revRange(0, 5).toArray(), new int[]{4, 3, 2, 1, 0});     assertArrayEquals(revRange(-5, 0).toArray(), new int[]{-1, -2, -3, -4, -5});     assertArrayEquals(revRange(1, 4).toArray(), new int[]{3, 2, 1});     assertArrayEquals(revRange(0, 0).toArray(), new int[0]);     assertArrayEquals(revRange(0, -1).toArray(), new int[0]);     assertArrayEquals(revRange(MIN_VALUE, MIN_VALUE).toArray(), new int[0]);     assertArrayEquals(revRange(MAX_VALUE, MAX_VALUE).toArray(), new int[0]);     assertArrayEquals(revRange(MIN_VALUE, MIN_VALUE + 1).toArray(), new int[]{MIN_VALUE});     assertArrayEquals(revRange(MAX_VALUE - 1, MAX_VALUE).toArray(), new int[]{MAX_VALUE - 1}); } 
vote vote


without external lib...

import java.util.List; import java.util.Collections; import;  public class MyCollectors {      public static <T> Collector<T, ?, List<T>> toListReversed() {         return Collectors.collectingAndThen(Collectors.toList(), l -> {             Collections.reverse(l);             return l;         });     }  } 

Top 3 video Explaining mvc - How can I add a class attribute to an HTML element generated by MVC's HTML Helpers?