【发布时间】:2015-01-30 18:22:01
【问题描述】:
我有一个新项目,我必须根据存储在数据库中的设置在视图中动态创建 HTML 控件。我想,可能有两种不同的方法
(1) 使用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) 使用 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" />
}
由于我是 MVC 新手,所以我无法决定。请大家提出最佳方法?
提前致谢
【问题讨论】:
标签: asp.net asp.net-mvc web-applications asp.net-mvc-5