Using a single view for mvc derived models

I have a model base class, NotificationBase, and two derived models, GeneralNotification and ReleaseNotification.

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; } } 

I am trying to use one editable view to edit both types of notifications.

There is a model of type NotificationBase in this view.

The problem is that I cannot get the added properties of derived types that will be displayed as edits. Submitting a model of a base type means that I lose as additional properties of derived types become lost.

Is there a workaround, or do I just need to make separate representations for each derived model?

+6
source share
4 answers

You can add several conditions to your view. Suppose your view is strongly typed with a base class:

 @model NotificationBase 

You can check for each subclass and add the appropriate fields (unverified code below!):

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

The same applies to the second subtype:

 @if (Model is ReleaseNotification) { Html.TextBoxFor(m => ((ReleaseNotification) m).Version); } 
+4
source

This can also be solved using the interface. For example, if you want to avoid conditions in your views. Razor views can also be strongly typed with an interface.

You can use the full interface implemented by NotificationBase with overrides in derived classes. (note that NotificationBase must be abstract for this). Or you implement partial interfaces.

Kr

0
source

Set the model type to the base type:

 @model NotificationBase 

Then:

 @Html.EditorForModel() 

EditorForModel is smart enough to display a form based on the specific type passed in at run time.

0
source

The problem is resolved: I passed the representation of a specific model of the derived type (GeneralNotification / ReleaseNotification) instead of passing the base model. Now the view has remained the same and eternal.

0
source

Source: https://habr.com/ru/post/950240/


All Articles