這是我第一次使用MVC,第一次編寫Web應用程序。MVC 4意見,共同還是特定?
到目前爲止,我設法看到員工列表以及Employee模型的編輯視圖。
如果我有25個模型需要顯示爲列表並進行編輯,我將不得不創建50個不同的視圖?
還是有辦法讓一個共同的列表視圖和一個常見的編輯視圖?
(編輯如下)
解決了列表視圖問題。 對不起,長碼。
我創建了一個描述模型屬性的類ModelPropertyInfo類。現在我只加了標籤,但我可能會增加更多的屬性,如「格式」,「的inputType」,...
// Model field information class. Used by views to display model info properly
public class ModelPropertyInfo
{
public ModelPropertyInfo() { }
public string Name { get; set; }
public string Label { get; set; }
}
然後ShowInListAttribute屬性類,裝飾,我想只出現模特屬性在列表視圖
// Attribute class used to specify Labels for model fields
public class ShowInListAttribute : Attribute
{
public ShowInListAttribute(string header)
{
Header = header;
}
public string Header { get; set; }
}
而一個模型庫類,我的所有車型都將繼承。本課程將給予通過將它的名字爲字符串
// Base class for all models
public class ModelBase
{
public static List<ModelPropertyInfo> ModelProperties(Type modelType)
{
List<ModelPropertyInfo> result = new List<ModelPropertyInfo>();
foreach (PropertyInfo pi in modelType.GetProperties())
{
ShowInListAttribute att = (ShowInListAttribute)pi.GetCustomAttributes(typeof(ShowInListAttribute), true).FirstOrDefault();
if (att != null)
result.Add(new ModelPropertyInfo { Label = att.Header, Name = pi.Name });
}
return result;
}
public object GetPropertyValue(string propName)
{
return this.GetType().GetProperty(propName).GetValue(this, null);
}
}
我們得到的類中的任何屬性值的能力,這是我員工模型類
[Table("Employee")]
public class Employee : ModelBase
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public decimal ID { get; set; }
[ShowInList("First Name")]
public string FirstName { get; set; }
[ShowInList("Last Name")]
public string LastName { get; set; }
public decimal DepartmentID { get; set; }
[ShowInList("Department")]
[DatabaseGeneratedAttribute(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
public string DepartmentName { get; set; }
}
所以,把所有上面的使用,這裏的索引方法在我的EmployeeController
public ActionResult Index()
{
ViewBag.Columns = ModelBase.ModelProperties(typeof(Employee));
ViewBag.Title = "Employee List";
return View("ListShared", db.Employees.ToList());
}
最後的結果,我會用它來顯示任何模型的列表SharedListView我想
@using SharedListView.Models
@model IEnumerable<ModelBase>
<h2>@ViewBag.Title</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
@foreach (ModelPropertyInfo col in ViewBag.Columns)
{
<th>
@col.Label
</th>
}
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
@foreach (ModelPropertyInfo col in ViewBag.Columns)
{
<td width='100px'>
@item.GetPropertyValue(col.Name).ToString()
</td>
}
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.GetPropertyValue("ID") }) |
@Html.ActionLink("Details", "Details", new { id=item.GetPropertyValue("ID") }) |
@Html.ActionLink("Delete", "Delete", new { id=item.GetPropertyValue("ID") })
</td>
</tr>
}
</table>
仍然停留在一個共同編輯觀點,任何幫助,將不勝感激。
再次,對於長編輯抱歉。
ContentFor(Model.Employee)? – gush
模型是Employee類型的,所以我認爲你的意思就像@ Html.ContentFor(Employee.Name)。我無法在任何地方罰款ContentFor Html助手。 – gush
對不起。我的意思是@ Html.ContentFor(Model) – thangchung