1

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>
}
}

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, thanks. I've heard of editor templates. I'll check them out instead of relying on the data type we store in the DB (I won't go into that).
0

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?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.