1

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 Answer 1

3

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) %>
Sign up to request clarification or add additional context in comments.

1 Comment

Do you have an example? For what he is describing, an Html Helper method with some styling is probably in order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.