我有一个类似于网格的表格,显示表格中的所有字段。 这是我的控制者:
public ActionResult Index()
{
DAL.DataManager dal = new DAL.DataManager();
List<LALegalLicensedata> data = new List<LALegalLicensedata>();
data = dal.get_LA_Get_AllDate();
return View(data);
}
这是我的观点:
@model IEnumerable<CSAProject.Models.LALegalLicensedata>
<table width="100%" class="display" id="example" cellspacing="0">
<thead>
<tr>
<th>Entity</th>
<th>License Type</th>
<th>State</th>
<th>Location</th>
<th>Data User</th>
</tr>
</thead>
<tbody>
@foreach(var item in Model)
{
<tr>
<td>@item.Entity</td>
<td>@item.License_Type</td>
<td>@item.State</td>
<td>@item.Location</td>
<td>@item.dataUser</td>
</tr>
}
</tbody>
</table>
同样在这个页面中我需要显示一个带复选框的optionList,其中包含Model中属性的名称,这是我的模型:
public class LALegalLicensedata
{
public int dataID { get; set; }
public string dataUser { get; set; }
public DateTime Create_Date { get; set; }
public DateTime Modified_Date { get; set; }
public string Modified_By { get; set; }
public string Status { get; set; }
}
这就是我从Model获取属性名称的方法:
LALegalLicensedata model = new LALegalLicensedata();
List<string> PropertyList = GetPropertiesNameOfClass(model);
public List<string> GetPropertiesNameOfClass(object pObject)
{
List<string> propertyList = new List<string>();
if (pObject != null)
{
foreach (var prop in pObject.GetType().GetProperties())
{
propertyList.Add(prop.Name);
}
}
return propertyList;
}
我需要在选项列表中显示一个PropertyList,我该怎么做?
这是显示和隐藏列的javascript和文本。我不喜欢静态文本,而是喜欢从属性中获取名称,并将它们放在带有复选框的选项列表中。
$(document).ready(function () {
var table = $('#example').DataTable({
"paging": true
});
$('a.toggle-vis').on('click', function (e) {
//e.preventdefault();
event.preventDefault ? event.preventDefault() : event.returnValue = false;
//Get the column API object
var column = table.column($(this).attr('data-column'));
// Toggle the visibility
column.visible(!column.visible());
});
});
</script>
<div>
Toggle column: <a class="toggle-vis" data-column="0">Entity</a> -
<a class="toggle-vis" data-column="1">License Type</a> -
<a class="toggle-vis" data-column="2">State</a> -
<a class="toggle-vis" data-column="3">Location</a> -
<a class="toggle-vis" data-column="4">Data User</a> -
<a class="toggle-vis" data-column="5">Create Date</a>
</div>
答案 0 :(得分:1)
您显示的模型与您显示的视图不匹配,因此假设您的模型实际上(与您显示的视图相匹配)
public class LALegalLicensedata
{
public string Entity { get; set; }
public string License_Type { get; set; }
public string State { get; set; }
public string Location { get; set; }
public string dataUser { get; set; }
}
然后,在Index()方法中,将属性名称添加到ViewBag
属性
....
ViewBag.PropertyNames = GetPropertiesNameOfClass(new LALegalLicensedata());
return View(data);
但我建议您使用包含属性List<LALegalLicensedata> Data
和List<string> PropertyNames
然后在视图中,您可以遍历集合以生成复选框
<div>
@foreach(var name in ViewBag.PropertyNames)
{
<label>
<input type="checkbox" class="toggle-column" checked />
<span>@name</span>
</label>
}
</div>
然后修改脚本以处理每个复选框的click()
事件
var checkBoxes = $('.toggle-column');
$('.toggle-column').click(function() {
var index = checkBoxes.index($(this));
$('tr th').eq(index).toggle();
$('tr td').eq(index).toggle();
});
请参阅this fiddle了解脚本的工作原理。
修改强>
您当前的GetPropertiesNameOfClass()
方法将返回您的案例中将显示的属性名称&#34; License_Type&#34;对于第二个属性,当我怀疑你可能希望它是&#34;许可类型&#34; (空格而不是下划线)。要解决此问题,请向属性添加[Display(Name = "License Type")]
,然后您可以使用以下方法
private List<string> GetDisplayNames(object model)
{
Type type = typeof(model);
List<string> displayNames = new List<string>();
foreach (var property in type.GetProperties())
{
var attributes = property.GetCustomAttributes(typeof(DisplayAttribute), true);
if (attributes.Length == 0)
{
displayNames.Add(property.Name);
}
else
{
displayNames.Add((attributes[0] as DisplayAttribute).Name);
}
}
return displayNames;
}
这也意味着您可以使用<th>@Html.DisplayNameFor(m => m.License_Type)</th>
生成表格标题而不是硬编码。