I got a new project in which I have to create HTML controls dynamically in a view based on a setting stored in a database. I thought,there could be two different approaches

(1) Using jQuery:

function bindControls(){
    $.ajax({
        type: "GET",
        url: "ViewDef/GetControls",
        contentType: "application/json; charset=utf-8",
        data: {},
        dataType: "json",
        success: function (response) {
            var res = response.d;
            if (res.trim() != "") {
                var data = $.parseJSON(res);
                $("#dvQuota").html("");
                var dvQuota = $("#dvQuota");
                $("#dvControls").addClass("ui-main");
                var cType = "";
                var cID = "";
                if (data.length > 0) {
                    $.each(data, function (i) {
                        cType = data[i].cType;
                        cID = data[i].cID;
                        switch (cType) {
                            case "TextBox":
                                $(dvQuota).append("<input type='text' id='" + cID + "' class='c1'/>");
                                break;
                            case "DROPDOWN":
                                $(dvQuota).append("<select type='text' id='" + cID + "' class='c1'></select>");
                                //logic for binding drop down list
                                break;
                        }

                    });
                }
            }
            }
        });
    }

2) Using C#

@using (Html.BeginForm())
{
    for (int i = 0; i < ds.Table[0].Length; i++)
    {
            <div>
                //logic for generating Controls
            </div>
    }
    <input type="submit" value="OK" />
}

As I am new to MVC, I'm unable to decide. Would request you all please suggest best approach?

Thanks in advance

Related posts

Recent Viewed