2013-07-25 6 views
6

私は基本モデルクラス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型のモデルがあります。

編集ビューに表示される派生型の追加されたプロパティを取得できないという問題があります。基底型のモデルを送信すると、派生した型の余分なプロパティを失うことになります。

回避策はありますか、派生モデルごとに個別のビューを作成するだけですか?

答えて

4

ビューにいくつかの条件を追加できます。あなたのビューが強く、基本クラスで型付けされているとします

@model NotificationBase 

あなたは各サブクラスをチェックし、対応するフィールドを追加します(以下の未テストコード!)することができます

@if (Model is GeneralNotification) 
{ 
    Html.TextBoxFor(m => ((GeneralNotification) m).Message); 
} 

同じことはもちろんの第二のサブタイプのために行きます:

​​
+0

クイックレスポンス、Andreiありがとうございます。それはまさに私がやっていることです、問題はこれは編集ビューであり、私は選択したエンティティのプロパティを表示する必要があります。 GeneralNotificationモデルでは、Messageプロパティの値を表示する必要があります。私の見解には伝えられないのはこの価値です。 –

+0

@OctavianEpure、どのようにモデルをビューに渡していますか?これのコードサンプルを共有できますか?理論上、 'return View(generalNotificationInstance)'を上の答えのスニペットとともに実行すると、期待した結果が得られるはずです。 – Andrei

+0

これは、@ Model NotificationBaseビューに渡すモデルです。コントローラでは、このNotificationBaseモデルをnullにします。 switch(typeId) { case(int)NotificationType.General: モデル=サービス。GetGeneralNotification(id); 休憩。 case(int)NotificationType.Release: model = Service.GetReleaseNotification(id); 休憩。 } リターンビュー(モデル); –

0

これは、インターフェイスを使用して解決することもできます。例えば、あなたの意見を避けるためです。レイザービューは、インターフェイスを使用して強く入力することもできます。

派生クラスのオーバーライドを使用して、NotificationBaseによって実装された完全なインターフェイスを使用できます。 (これに対して、NotificationBaseは抽象的である必要があることに注意してください)。あるいは、部分的なインタフェースを実装します。

のKr

0

は基本型にモデルタイプを設定します。

@model NotificationBase 

その後:

@Html.EditorForModel() 

EditorForModelは、実行時に渡されたコンクリートの種類に基づいてフォームをレンダリングするのに十分なスマートです。

+0

StarDateフィールドとEndDateフィールドのビューに特別なJAvaScript日時エディタを表示する必要があるため、EditorForModelを使用できません。 –

+1

これは、正確には、「EditorTemplates」のためのものです。ジェネリック 'DateTime'エディタテンプレート('〜/ Views/Shared/EditorTemplates/DateTime.cshtml')を追加するか '' StartDate'& 'EndDate'プロパティを' [UIHint( "JsDateTime")] 'で修飾して'〜/ビュー/共有/ EditorTemplates/JsDateTime.cshtml'。 – haim770

0

問題解決:基本型モデルを渡す代わりに、特定の派生型モデル(GeneralNotification/ReleaseNotification)をビューに渡しました。この展望は今も変わらず、永遠に続く作品になっています。

+0

しかし、かみそりのページで余分なプロパティを編集するには、@ Htlm.Textbox( "propName"、propValue)のようなベースカミソリの構文を使用しますか? – TypingPanda

+0

以下の@Andreiから回答があると思います。ありがとう – TypingPanda

関連する問題