John John
John John

Reputation: 1

Unable to apply a css class to my @Html.DisplayFor inside a WebGrid, inside my asp.net MVC 5 web application

I have the following code inside my asp.net MVC Razor view:-

 @{
    List<Marketing.Models.SalesData> allLevels = ViewBag.AllLevels;
    var gridcolumns = new List<WebGridColumn>();
    gridcolumns.Add(new WebGridColumn()
                {
                    ColumnName = "Status",
                    Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().Status).ToString(),
                    CanSort = true,
                    Format =

                        (item) =>
                        {
                            var banner = item.Value as Marketing.Models.SalesData;
                            return Html.DisplayFor(modelItem => banner.Status, new { @class = banner.Status });
                        }
                });

now what i am trying to do is to define a css class inside my Html.DisplayFor(modelItem => banner.Status, new { @class = banner.Status }); but this will render the without any css class, as follow:-

<td>succeed</td>

now i though the problem with the way i am defining the class, so i tried this test, to hard code the class name Html.DisplayFor(modelItem => banner.Status, new { @class = "testclass"}); but this did not render the css class.. so can anyone adivce on this please? now i am using asp.net mvc version 5.2.3.0 which should support defining css classes inside my HTML helpers.

Upvotes: 1

Views: 930

Answers (1)

rased
rased

Reputation: 136

At first take a look to this example. @Html.LabelFor() Helper function renders a <label></label> tag. Suppose if you write @Html.LabelFor(m=>m.Name, new {@class = "form-control"}) it returns <label class = "form-control">David</label>.So we can apply class to label for helper function. But @Html.DisplayFor() do not render any html tag. If you write @Html.DisplayFor(m=>m.Name) it returns your name only, like: David. So you have to wrap @Html.DisplayFor() Helper inside a span and apply class to it. Like: <span class = "form-control">@Html.DisplayFor(m=>m.Name)</span>. Then output will be <span class="form-control">David</span> . That means class applied on name property. Hope this helps..

Upvotes: 1

Related Questions