私は基本モデルクラスNotificationBaseと2つの派生モデルGeneralNotificationとReleaseNotificationを持っています。派生mvcモデルの単一ビューの使用
public class NotificationBase
{
public int Id { get; set; }
[Required]
[StringLength(50, ErrorMessage="Title must not exceed 50 characters.")]
public string Title { get; set; }
[Required(ErrorMessage="Type is required.")]
public int TypeId { get; set; }
[Required(ErrorMessage="Importance is required.")]
public int ImportanceId { get; set; }
public DateTime Created {get; set; }
[Required(ErrorMessage="Start date is required.")]
public DateTime StartDate { get; set; }
[Required(ErrorMessage="End date is required")]
public DateTime EndDate { get; set; }
[AllowHtml]
[Required(ErrorMessage="Details are required")]
public string Details { get; set; }
}
public class GeneralNotification : NotificationBase
{
[Required(ErrorMessage="Message is required.")]
[StringLength(50, ErrorMessage = "Message must be maximum 50 chararacters long.")]
public string Message { get; set; }
}
public class ReleaseNotification : NotificationBase
{
[Required(ErrorMessage="Version is required.")]
public string Version { get; set; }
}
私は両方の派生通知タイプを編集するために、単一の編集ビューを使用しようとしています。
このビューには、NotificationBase型のモデルがあります。
編集ビューに表示される派生型の追加されたプロパティを取得できないという問題があります。基底型のモデルを送信すると、派生した型の余分なプロパティを失うことになります。
回避策はありますか、派生モデルごとに個別のビューを作成するだけですか?
クイックレスポンス、Andreiありがとうございます。それはまさに私がやっていることです、問題はこれは編集ビューであり、私は選択したエンティティのプロパティを表示する必要があります。 GeneralNotificationモデルでは、Messageプロパティの値を表示する必要があります。私の見解には伝えられないのはこの価値です。 –
@OctavianEpure、どのようにモデルをビューに渡していますか?これのコードサンプルを共有できますか?理論上、 'return View(generalNotificationInstance)'を上の答えのスニペットとともに実行すると、期待した結果が得られるはずです。 – Andrei
これは、@ Model NotificationBaseビューに渡すモデルです。コントローラでは、このNotificationBaseモデルをnullにします。 switch(typeId) { case(int)NotificationType.General: モデル=サービス。GetGeneralNotification(id); 休憩。 case(int)NotificationType.Release: model = Service.GetReleaseNotification(id); 休憩。 } リターンビュー(モデル); –