non factors MVC core create dynamic html

1.7k Views Asked by At

I am working on asp.net core application and using nonfactors webgrid.

In some scenario I need to create different html tag for true and false value of model property.

For true => any div tag

For false => any image tag

Anybody have any idea regarding this?

2

There are 2 best solutions below

0
On

You'll need to familarize yourself with control structures.

1
On

I use the NonFactors MVC Grid frequently, I'm a little confused on your question but I'll answer what I think I read.

Each field can be rendered however you see fit.. when a field is true you could render a div tag with whatever in it, when false an image tag with something else. Here is an example using that grid (I am using a User model as the example.. so that Model.Users is an IEnumerable of that model):

    @(Html
        .Grid(Model.Users)
        .Build(columns =>
        {
            columns.Add(model => model.username).Titled("Username");
            columns.Add(model => model.first_name).Titled("First Name");
            columns.Add(model => model.last_name).Titled("Last Name");
            columns.Add(model => model.last_login).Titled("Last Login").RenderedAs(model => model.last_login == DateTime.MinValue ? "Never" : model.last_login.ToString());
            columns.Add(model => model.banned).Titled("Banned").Encoded(false).RenderedAs(model => model.banned ? "<div>whatever goes here</div>" : "<img />");
        })
        .Filterable()
        .Sortable()
        .Pageable()
        .Css("table table-sm table-striped table-hover table-responsive")
    )