特性可向程序中添加元数据。 Metadata is information about the types defined in a program.” data-guid=”01e24c05eb8a815d9d031ae958a894ae” style=”margin: 0px; padding: 0px;”>元数据是有关在程序中定义的类型的信息。 所有的 .NET 程序集都包含指定的一组元数据,这些元数据描述在程序集中定义的类型和类型成员。 可以添加自定义特性,以指定所需的任何附加信息。
public class DetailAttribute : Attribute { public string AttrName { set; get; } public string Html { set; get; } public string DefaultValue { set; get; } public string DataSource { set; get; } }
对应的Model:
public class Factory { [Detail(AttrName='宽度', Html='', DefaultValue='50', DataSource=null)] public string Width { set; get; }
[Detail(AttrName = '高度', Html = '', DefaultValue = '50', DataSource = null)] public string Height { set; get; }
[Detail(AttrName = '状态', Html = '', DefaultValue = null, DataSource = 'select text,value from status')] public string Status { set; get; }
[Detail(AttrName = 'Tag值', Html = '', DefaultValue = '', DataSource = null)] public string Tag { set; get; } }
public class FactoryDetail { [Detail(AttrName = '宽度', Html = '', DefaultValue = '50', DataSource = null)] public string Width { set; get; }
[Detail(AttrName = '高度', Html = '', DefaultValue = '50', DataSource = null)] public string Height { set; get; }
[Detail(AttrName = '状态', Html = '', DefaultValue = null, DataSource = 'select text,value from status')] public string Status { set; get; }
[Detail(AttrName = 'Tag值', Html = '', DefaultValue = '', DataSource = null)] public string Tag { set; get; } [Detail(AttrName = '描述', Html = '', DefaultValue = '', DataSource = null)] public string Desc { set; get; } }
然后在界面的拖放事件结束时通过js发送ajax请求来得到界面要呈现的html:
$('.jq-draggable-outcontainer').draggable({ helper: 'clone', scroll: true, drag: function (event, ui) { // debugger; } }); $('#content').droppable({ drop: function (event, ui) { // debugger; if (ui.draggable[0].className.indexOf('jq-draggable-outcontainer') > 0) { var text = ui.draggable[0].innerText.trim(); $(this).append('
' + text + '
');
$('#content2').html(''); cur_selector = $('#window'+iIndex); $.Ewin.AjaxPost('/Home/GetModelByType', { strType: 'Factory' }, function (data, status) { var element = $.parseJSON(data.Json); var arrProp = element.element.property; //0.构造html var strHtml = '
'; //1.拼html构造属性 strHtml += '
'; $('#content2').append(strHtml); }, function () {
}, null); iIndex++;
} } });
对应的C#方法:
public JsonResult GetModelByType(string strType) {
//strType传过来的是Factory或者FactoryDetail var assembly = Assembly.Load('Ewin.Client.Web'); //参数为程序集的名称 var oType = assembly.GetType('Ewin.Client.Web.Controllers.' + strType);
//得到类的所有属性 var lstProperties = oType.GetProperties(); foreach (var oProperty in lstProperties) {