MVC を使用するのはこれが初めてで、Web アプリケーションを作成するのも初めてです。
ここまでで、従業員のリストのビューと、Employee モデルの編集ビューを作成できました。
リストとして表示して編集する必要がある 25 個のモデルがある場合、50 個の異なるビューを作成する必要がありますか?
または、1 つの共通のリスト ビューと 1 つの共通の編集ビューを持つ方法はありますか?
(以下を編集)
リスト ビューの問題を解決しました。長いコードで申し訳ありません。
モデル プロパティを記述するModelPropertyInfoクラスを作成しました。今のところ、Label のみを追加しましたが、"Format"、"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; }
}
そして、すべてのモデルが継承するModelBaseクラス。このクラスは、その名前を文字列として渡すことにより、クラスからプロパティ値を取得する機能を提供します
// 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);
}
}
さて、これが私のEmployeeモデルクラスです
[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の Index メソッドです。
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>
まだ一般的な編集ビューにこだわっていますが、助けていただければ幸いです。
繰り返しますが、長い編集で申し訳ありません。