0

In my model I have a property called Reasons. I want the user to be able to select from a small list of reasons. The list will look something like this:

PR   Promotional

CL   Customer Related

SL   Supplier Related

I have tried using an enum for this, but I can't seem to get it working. Intellisense won't recognize Reasons as a property of my model when I try to hook it up with my model (yes, it is set to public). What would be the best approach to this? I definitely don't think using a database is necessary in this case and I'd rather avoid using one. Thanks for any help. I'm using ASP.NET MVC 5.

EDIT-- Update on the issue

After some more testing, I've realized that the issue is occurring because of the way that my ViewModel is set up. My ViewModel contains a property that references one of my other models, which references all of the other models. I have written the classes with the objective of serializing them to XML based on a predetermined XML structure. Here is an example:

<MyViewModel>
    <Property>
    </Property>
    <ReferenceToOtherModel>
        <OtherModel1>
            <ReferenceToOtherModel2>
            </ReferenceToOtherModel2>
        </OtherModel1>
    </ReferenceToOtherModel>
</MyViewModel>

To summarize this, MyViewModel is the root of the XML structure, so I am unable to put a property in the ViewModel itself that serves as a link to the enum.

EDIT 2:

The error message I'm getting is something like this: "model.otherModel1.otherModel2.Reasons is a type, which is not valid in the given context. Reasons: cannot reference a type through an expression; try 'otherModel2.Reasons' instead." Unfortunately, othermodel2 cannot be referenced without fully qualifying the name, which goes back to the first sentence of the error message.

ic3man7019
  • 721
  • 6
  • 24

1 Answers1

0

Assuming you have your enum like this

public enum Reasons
{
    [Display(Name = "Promotional")]
    Promotional,

    [Display(Name = "Customer Related")]
    CustomerRelated,

    [Display(Name = "Supplier Related")]
    SupplierRelated
}

and your view model has a property of this type

public class YourViewModel
{
   public Reasons Reason { set; get; }    
}

You can use Html.EnumDropDownListFor helper method

@model YourViewModel
@using (Html.BeginForm("Create", "Issue", FormMethod.Post))
{
    @Html.EnumDropDownListFor(s=>s.Reason, "select one")
}

I just used Promotional instead of PR as that is more readable to me. You can update that part as needed.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I have edited the question. I tried to explain the issue to the best of my ability, but I don't know if it's clear. Basically, where you have `public Reasons Reason {get; set;}`, is where my class hierarchy gets in the way. I can't put that property in the ViewModel itself. – ic3man7019 Oct 30 '17 at 16:28
  • What you mean you are unable to put a property in the view model ? If you need a prop to represent the reason, you need to add a property. – Shyju Oct 30 '17 at 16:31
  • Put another way, my ViewModel has a property that references another model, which references another model, which is the model that contains `public enum Reasons`. – ic3man7019 Oct 30 '17 at 16:31
  • I've updated the question to be more clear about the issue. Hopefully that helps. If not, thanks for your time. I appreciate it. – ic3man7019 Oct 30 '17 at 16:46
  • Ideally, your view model should be a class to transfer data between your view and your action method. The view model should be very specific to the view. and if you follow that concept, you will get lean-flat class,without multiple nested levels. Looks like you do not have that. I guess that is where you need to start – Shyju Oct 30 '17 at 16:59