如何在MVC视图的ListBox中显示字符串列表
本文关键字:显示 字符串 列表 ListBox MVC 视图 | 更新日期: 2023-09-27 18:23:36
我有一个类似于网格的表,它显示了表中的所有字段。这是我的控制器:
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; }
}
这就是我如何从模型中获得属性名称:
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>
您显示的模型与您显示的视图不匹配,因此假设您的模型实际上是
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();
});
请参阅这个小提琴了解剧本的工作原理。
编辑
您当前的GetPropertiesNameOfClass()
方法将返回属性名称,在您的情况下,该名称将显示第二个属性的"License_Type",而我怀疑您可能希望它是"License Type"(空格而不是下划线)。要解决此问题,请在属性中添加[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>
生成表格标题,而不是硬编码。