views:

859

answers:

2

Hello Everyone,

In my current application we create controls dynamically inside panel based on database value. Like, Type controls, Style, width, etc. Is it possible to do something like this using ASP.NET MVC?

Thanks, Alps

+1  A: 

ASP.Net MVC doesn't use server controls like ASP.Net webforms does.

What you're talking about is definitely possible, but MVC gets you down to the HTML level, rather than abstracting it into controls.

You'd likely want to be using partial views, or else looking at adding extension methods to the HTMLHelper class to help you generate dynamic content.


Here's a very simple example HtmlHelper extension method. It's simple, for sure, but you can see how it would be easy to expand it to output the dynamic html you'd need. This method takes an input value, and outputs no html if it's null, the value plus a "<br>" tag if "addBr" is set to true, or just the value if "addBr" is false.

public static string FieldOrEmpty(this HtmlHelper<T> helper, 
                                     object value, bool addBr) 
        {
            if (value == null)
            {
                return string.Empty;
            }
            else if (addBr)
            {
                return value.ToString() + "<br />";
            }
            else
            {
                return (value.ToString());
            }
        }
    }

You'd call this in your View with

<%= HtmlHelper.FieldOrEmpty(Model.Field1) %>
womp
Do you have an example? For what he is describing, an Html Helper method with some styling is probably in order.
Robert Harvey
Good idea... done......
womp
A: 

I'VE TRIED BUT DOESN'T WORK. DO YOU HAVE AN WORKING EXAMPLE. THANKS,

XXX