0

Here is a ViewModel I have created for UpdatingApplication

using System.Threading.Tasks;

namespace MEMineralResources.Common.Models.Application
{

  public class EditApplicationModel
  {
      public int DevisionId { get; set; }


    public int ApplicationId { get; set; }


  Required(ErrorMessageResourceType = typeof(MEMineralResources.Resources.Resources),
     ErrorMessageResourceName = "ErrorMessageObligatory")]
    public List<ResourceMineral> ListResourceMinerals { get; set; }


          [Required(ErrorMessageResourceType = typeof(MEMineralResources.Resources.Resources),
          ErrorMessageResourceName = "ErrorMessageObligatory")]
    [Display(Name = "MainUser", ResourceType = typeof(Resources.Resources))]
    public int MainUserId { get; set; }


          [Required(ErrorMessageResourceType = typeof(MEMineralResources.Resources.Resources),
          ErrorMessageResourceName = "ErrorMessageObligatory")]
    [Display(Name = "HelperUser", ResourceType = typeof(Resources.Resources))]
    public int HelperUserId { get; set; }


     [Required(ErrorMessageResourceType = typeof(MEMineralResources.Resources.Resources),
          ErrorMessageResourceName = "ErrorMessageObligatory")]
    [Display(Name = "HelperUser", ResourceType = typeof(Resources.Resources))]
    public int HelperUser2Id { get; set; }


        [Required(ErrorMessageResourceType = typeof(MEMineralResources.Resources.Resources),
        ErrorMessageResourceName = "ErrorMessageObligatory")]
    [Display(Name = "PloshtNahodishte")]
    public decimal AreaSize { get; set; }

    [Required(ErrorMessageResourceType = typeof(MEMineralResources.Resources.Resources),
        ErrorMessageResourceName = "ErrorMessageObligatory")]
    public string SelectedAreaTypeID { get; set; }

    //a lot other properties which are common for all divisionIDsa


  }
}

SO So I've got a links like his - Application/UpdateApplciation/?divisionID=2

So depending on this property ehich you see also in teh model - divisionId I should return a view from this viewmodel but slightly different depending on the divisionID

Some of the differences are

  1. when divisionID=2 or 3 - ListResourceMineral - is not obligatory and AreaSize - is not required and I will hide it in the model (it doesn't exist)

  2. When divisionID=5 - HelperUser and HelperUser2Id are not required also a problem is that Displplay Property is with different value - strange but true.

/ SO I think about Creating Custom Attribute that will be something similar RequireIf(DivisionID=5,6,7)

or the other way is in the code of the when DivisionID = 2 or 3 to remocve the error from the model state

this.ModelState.Remove("AreaSize");

Can you suggest cleaner or better approaches to this problem. Best Regards

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Tania Marinova
  • 1,788
  • 8
  • 39
  • 67
  • 2
    Possible duplicate of [Possible to change Data Annotations during Runtime? (ASP.NET MVC's \[Range\] \[Required\] \[StringLength\] etc.)](http://stackoverflow.com/questions/4088274/possible-to-change-data-annotations-during-runtime-asp-net-mvcs-range-requ) – Hamlet Hakobyan Jan 02 '16 at 09:24
  • This is probably what you need: [stackoverflow-attribute-dependent-on-another-field](http://stackoverflow.com/questions/3713281/attribute-dependent-on-another-field) – erikscandola Jan 02 '16 at 10:16
  • 1
    I think you should use "FluentValidations" NuGet.. By using FluentValidations, you won't need to use any of the data annotations for validations. Rather, this provides a much cleaner and manageable way to specify your validations. And you can write unit tests for your validations if you wish to. – Nirman Jan 02 '16 at 13:44
  • I think that you should go with your solution. secondly you ask for the more better sol in the long run is to create different viewmodels. – maztt Jan 02 '16 at 13:56

0 Answers0