ewomack
ewomack

Reputation: 753

MVC Razor Rendering controls dynamically

This is one way I've found to render controls dynamically with ASP.NET MVC 3 Razor. This is giving me correct data, but I'm curious if anyone sees any red flags with this method, or a painfully more obvious way to do this.

@using (Html.BeginForm())
{
foreach (var item in Model)
{
    <tr>
        <td>
            @item.app_name
        </td>
        <td>
            @item.setting_name
        </td>
        <td>
            @item.setting_description
        </td>
        <td>
             @if (item.data_type == "Bit")
             {
                @Html.CheckBox("setting_value", item.setting_value == "1" ? true : false)
             }
             else
             {
                @Html.TextBox("setting_value", item.setting_value)
             }
        </td>
        <td>
            @item.setting_value
        </td>
    </tr>
}
}

Upvotes: 1

Views: 3196

Answers (2)

Ed DeGagne
Ed DeGagne

Reputation: 3269

What do editor templates have to do with dynamically creating controls?

What if you need to drive a UI/View from settings in a database, for example?

Upvotes: 0

cwharris
cwharris

Reputation: 18125

You could use Editor and Display Templates instead...

Check out this link:

http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx

Upvotes: 1

Related Questions