dropdownlist of mvc3 operators

Is there a better way to implement a dropdown list in MVC3 that looks like the one in this HTML/JS example?

I attempted to achieve this using the following code:

Model:

public enum Operator
{
    GreaterThan = '>',
    LessThan = '<',
    Equal = '='
}

public Operator SelectedOperator { get; set; }

View Model:

@Html.DropDownListFor(model => model.SelectedOperator, new SelectList(Enum.GetValues(typeof(YourNamespace.Models.Operator))))

View:

@Html.EditorFor(x => x.AdvanceSearchModel)

While this gave me a basic dropdown list with "Greater Than," "Less Than," and "Equal," I am unsure how to display "<," ">,=" in the dropdown list. Any suggestions?

Answer №1

@Html.DropDownListFor(
    model => model.getOp,
    ((Fnx.Esb.ServiceMonitor.ViewModel.AdvanceSearchModel.oporetor[])Enum.GetValues(
        typeof(Fnx.Esb.ServiceMonitor.ViewModel.AdvanceSearchModel.oporetor)
    )).Select(x => new SelectListItem
    {
        Value = x.ToString(),
        Text = ((char)x).ToString()
    })
)

You could also optimize the code by directly preparing the data in your view model:

public enum Operator
{
    greater_then = '>',
    less_than = '<',
    equal = '='
}

public class AdvanceSearchModel
{
    public IEnumerable<SelectListItem> Operators 
    {
        get
        {
            return ((Operator[])Enum.GetValues(typeof(Operator)))
                .Select(x => new SelectListItem
                {
                    Value = x.ToString(),
                    Text = ((char)x).ToString()
                });
        }
    }

    public Operator GetOp { get; set; }
}

Then, in your view, you can simply use:

@Html.DropDownListFor(model => model.GetOp, Model.Operators)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with Angular JS's ngRoute feature not functioning properly

I've been struggling with my ngRoute implementation. I seem to be unable to load the 'ngRoute' module for some reason. Whenever I run this code, I just end up with a blank page. Below is my app.js file: var app = angular.module('tutor ...

Using Polymer and D3: A guide to transferring JSON data from element attributes to templates

As a newcomer to polymer, I wanted to integrate D3 with it. After modifying an existing Gist, I encountered some challenges. My goal is to pass JSON from a Polymer element attribute (barData) in HTML to the polymer template. The JSON is passed as a String ...

How can I simultaneously add two elements to a single node or to a node that does not exist in the JSON file?

I thought this would be simple, but I'm struggling to find the answer... var crit = { 'webshopId': webshopId, 'type': 'product'} }; I need to include sku.regularPrice = { $gte : parameters.minPr ...

CAUTION: Attempted to load angular multiple times while loading the page

I encountered a warning message while working on my project, causing errors in calling backend APIs due to duplicate calls. Despite attempting previously suggested solutions from the forum, I am stuck and seeking assistance. Can anyone provide guidance? Be ...

Steps to update XmlHttpRequest URL during image upload

I am currently facing an issue with updating images on my website. When I try to update an image, it redirects to the wrong URL instead of the intended one. The form is set to post data to this URL: POST http://127.0.0.1/mgt/upload/processImage/foodImage ...

How can elements with class prefix names be retrieved in IE Browser Helper Object using c#?

I've been developing an IE Plugin using BHO and I need to access an element based on its class prefix in C#. In JavaScript and jQuery, I was able to accomplish this with the following code: var myClass = $('[class^="ii gt"]'); and var myC ...

I'm curious as to why window.opener is null in a popup when utilizing Google OAuth, and what is the most effective way to transfer the access token to the parent window

In my React application, I am working with Google OAuth and implementing it through a popup. The setup involves using Passport with my own custom backend. I start by supplying the initial URL to the popup window, which is the entry point on my backend that ...

Leverage all the documents obtained from a collection to reference in a Vue component

I am attempting to display all documents from a Firestore collection in a table using refs, but I am unsure about accessing each field for template ref. getDocs(collection(db, "usr")) .then((querySnapshot) => { querySnapshot.forEach((doc ...

Passing the app variable from Express.js to routes

I am attempting to transfer some data from the app (variable defined as var app = express();) to some Socket.IO related code by sending a value to a middleware in a similar manner: function routes(app) { app.post('/evento', function (req, re ...

Tips for toggling a menu by clicking a link

I have a Navbar component with a hamburger menu. When the hamburger menu is clicked, I want to display the menu component, which is separate. To achieve this, I passed data through props and made it work. Now, I want the menu to close when clicking outsi ...

Creating Virtual Nodes from a string with HTML tags in Vue 2.5: A Step-by-Step Guide

Having some trouble creating a functional component that displays the Feather Icons package - I'm stuck on the final step. Here's what I have so far: This is my FeatherIcon.vue component. <script> const feather = require("feather-icons"); ...

The data-tooltip feature displays information as [Object object]

There seems to be an issue with displaying text in the data-tooltip. Instead of showing the intended message, it is displaying [Object object]. import React from "react"; import { FormattedMessage } from "react-intl"; const translate = (id, value={}) = ...

The absence of a flickering flame is noticeable in the THREE.js environment

I have been working on creating a flame using THREE.js and spark.js. However, even after rendering the world, I am unable to see the flame and it seems like the world is empty. Although I checked the console for errors, there are no indications of what mig ...

a versatile tool or JavaScript module for dissecting different HTML documents

Looking to develop a versatile wrapper or JS plug-in that can parse HTML content and locate tables within the files, allowing users to generate visual graphs like pie charts and bar graphs. The challenge lies in the fact that the HTML structure is not fixe ...

Is it possible to convert an array into an object?

I'm attempting to restructure an array of objects into a new object where the label property serves as the key for multiple arrays containing objects with that same label. Check out this JSBin function I created to map the array, but I'm unsure ...

How can I position text below a ticked checkbox?

Having an issue with my HTML and jQuery code. Everything is working fine, but when I click on a checkbox, the text "FINISHED" appears below all checkboxes instead of just the one I clicked on. This is my html code: $('.label__checkbox').cli ...

Steps for embedding a custom function in a switch statement

I am attempting to run a switch statement based on the argument provided to the function below. I have been trying to call different functions depending on the argument passed. However, I encountered an Uncaught ReferenceError in the beginning of the .js f ...

Does this Spread Operator Usage Check Out?

Upon reviewing Angular's API documentation, I came across the declaration for the clone() method in HttpRequest as follows: clone(update: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" ...

Tips for concealing the "maxlength" attribute in HTML

Here is the code snippet I currently use on a signup and login form to restrict users from entering more than a specified number of characters: $( "#limit1" ).attr('maxlength','11').on('input', function() { if ($(this).val(). ...

Manipulating the length of an array based on a specified range using Vue.js

I'm currently working on a client's range filtering feature using Vue.js. The filter involves an input element with the type range to adjust the total number of clients displayed. I have successfully linked the value of the input to the **clients ...