Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:47:13+00:00 2026-06-12T12:47:13+00:00

I am creating a select list which is populated with enum values: <%= Html.DropDownListFor(model

  • 0

I am creating a select list which is populated with enum values:

<%= Html.DropDownListFor(model => model.OrderStatus, new SelectList(Enum.GetValues(typeof(OrderStatus))))%>

I am now performing client-side validation to ensure that OrderStatus is set properly:

[DisplayName("Order Status"), EnsureOrderStatus("ID")]
public OrderStatus OrderStatus { get; set; }


public class EnsureOrderStatus : ValidationAttribute, IClientValidatable
    {
        private readonly string OrderIDPropertyName;

        public EnsureOrderStatus(string orderIDPropertyName)
        {
            OrderIDPropertyName = orderIDPropertyName;
        }

        protected override ValidationResult IsValid (object value, ValidationContext validationContext)
        {
            ValidationResult validationResult = ValidationResult.Success;

            var propertyTestedInfo = validationContext.ObjectType.GetProperty(OrderIDPropertyName);
            if (propertyTestedInfo == null)
            {
                validationResult = new ValidationResult(string.Format("Unknown property {0}", OrderIDPropertyName));
            }
            else
            {
                int orderID = (int)propertyTestedInfo.GetValue(validationContext.ObjectInstance, null);
                OrderStatus orderStatus = (OrderStatus)value;

                if (orderID == 0 && orderStatus != OrderStatus.Future)
                {
                    validationResult = new ValidationResult(string.Format("Order must have have an Order Status of Future when being created."));
                }
            }

            return validationResult;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
                {
                    ErrorMessage = ErrorMessage,
                    ValidationType = "ensureorderstatus"
                };

            rule.ValidationParameters["orderid"] = OrderIDPropertyName;
            yield return rule;
        }
    }

The value of ‘value’ inside of IsValid is always the first enum value of OrderStatus. I was wondering if there was other steps which need to be taken to ensure that the selected value of a DropDownList element updates properly.

UPDATED CODE:

//OrderDetailsModel:
[DisplayName("Order Status"), EnsureOrderStatus("ID")]
public OrderStatus OrderStatus { get; set; }
public List<OrderStatusModel> OrderStatusModels = new List<OrderStatusModel>();

public OrderDetailsModel()
{
    OrderStatusModels.Add(new OrderStatusModel { EnumStatus = OrderStatus.Active, StringStatus = "Active" });
    OrderStatusModels.Add(new OrderStatusModel { EnumStatus = OrderStatus.Completed, StringStatus = "Completed" });
    OrderStatusModels.Add(new OrderStatusModel { EnumStatus = OrderStatus.Future, StringStatus = "Future" });
}


<%= Html.DropDownListFor(model => model.OrderStatus, new SelectList(Model.OrderStatusModels, "StringStatus", "EnumStatus"))%>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:47:14+00:00Added an answer on June 12, 2026 at 12:47 pm

    I would rethink this.. personally. Trying to hack an enum into a SelectList will create more work than is required.

    Simply, wrap this all in a ViewModel.. consider this:

    public enum OrderStatus {
        NOT_SHIPPED,
        SHIPPED
    }
    
    public class ViewModel {
        public OrderStatus SelectedStatus { get; set; }
        public List<StatusViewModel> Models = new List<StatusViewModel>();
    }
    
    public class StatusViewModel {
        public string StringStatus { get; set; }
        public OrderStatus EnumStatus { get; set; }
    }
    

    Setup (perhaps in the controller.. or somewhere else) involves this:

    ViewModel model = new ViewModel();
    
    model.Models.Add(new StatusViewModel() { EnumStatus = OrderStatus.NOT_SHIPPED, StringStatus = "Not shipped" });
    model.Models.Add(new StatusViewModel() { EnumStatus = OrderStatus.SHIPPED, StringStatus = "Shipped" });
    
    return View(model);
    

    ..and your view is simply this:

    @model Models.ViewModel
    @Html.DropDownListFor(x => x.SelectedStatus, new SelectList(Model.Models, "StringStatus", "EnumStatus"))
    

    Then, when your model comes in after being posted, the SelectedStatus is strongly typed OrderStatus enum:

    public ActionResult Index(ViewModel model) {
        // model.SelectedStatus is an OrderStatus
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a select box for a form using this in _form.html.erb <%=
When creating a new project in netbeans, if i select JAVA Desktop application, it
In my form fancy select boxes are used. Which resulting in creating some spans
I am creating a chunk of HTML/JavaScript with the below code: $result = mysql_query(SELECT
I am creating an edit form for an Event data model in which the
I have the following JQuery which allows a user to select/de-select a list of
I was creating a form like this : if select b, the textbox is
When creating a subscription on reporting services 2008 one is unable to select all
I am creating a file from a SELECT query using sqlplus with SPOOL command.
I am creating a jQuery Mobile page that has two select menu's, one is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.