-1

View

<input type="hidden" name="selectedValue" value="0" />
            <select name="itemType" id="itemType" style="width:80%;">
                <option value="Item1">Item 1</option>
                <option value="Item2">Item 2</option>
                <option value="Item3">Item 3</option>
                <option value="Item4">Item 4</option>
                <option value="Item5">Item 5</option>
            </select>

ViewModel

public ItemTypes itemType { get; set; }

Enum List in ItemTypes.cs (Extension Method)

public enum ItemTypes
{
    Item1,
    Item2,
    Item3,
    Item4,
    Item5
}


public static string GetItemDesc(this ItemTypes itemtype)
    {
        switch (itemtype)
        {
            case ItemTypes.Item1:
                return "Item 1 Description";

            case ItemTypes.Item2:
                return "Item 2 Description";

            case ItemTypes.Item3:
                return "Item 3 Description";

            default:
                return "Item 4 and 5 Description";
        }
    }

Above is my code. I would like to retain the selected Enum value throughout the page. I have four, The Index (where the drop down menu lives), the page where you select your payment method, the verification page, (where you verify all the information you put in) and the receipt page (to show that your transaction has been successful.) I need the enum value to remain the same through each page. Please help.

Cecily
  • 61
  • 1
  • 5
  • Give your enum a value Item1 = 0, Item2 = 1 etc and change the values in your dropdown to correspond to these values and then pass the integer around. You can easily parse the enum value back – InitLipton Jun 14 '16 at 16:02
  • 1
    Also a nicer way to add a description to your Enum is to use the Description attribute. [Description("Item 1 Description")] above the enum value and use an extension method that will read that attribute. Theres alot of examples online. http://stackoverflow.com/questions/2650080/how-to-get-c-sharp-enum-description-from-value – InitLipton Jun 14 '16 at 16:05
  • Your view model should be handling this via model binding assuming the dropdown list is inside a form (which it should be) – DGibbs Jun 14 '16 at 16:08
  • Thank you, could you please show me an example of parsing the enum value back? I am very new to MVC. – Cecily Jun 14 '16 at 16:13

2 Answers2

1

It looks to me like what you need to do is to save the selection somewhere, so the other pages can access that selection. MVC is stateless, so the value needs to either be passed along in each call, or it needs to be saved where the other pages can access it. There are several approaches you could take to save and retrieve selections of browser controls.

Keep in mind that enums will serialize to an integer by default, so when dealing with the value on the client-side, it will be an integer. What this means is that on your view, you likely need to use <option value="0">Item 1</option> using 0, 1, 2, 3, and 4 as your client-side values - or use the MVC HTML helpers or Razor markup, depending on your MVC version, to have those options generated for you using the enum names.

Here are some saving/retrieving options that may fit your needs:

  • Pass the selection value as a property of the posted content (your itemType) when you submit each page, then when you load the next page, include the value in the query string, or as part of the route for the next get request (GetPaymentMethodPage, GetVerifyPage, GetReceiptPage).

  • Save the selection value in a cookie, and retrieve it from that cookie in JavaScript (you'll need to provide a default if the cookie doesn't exist, was deleted, or the user doesn't allow cookies).

  • Save the selection value in browser storage - there are various kinds including Web Storage (SessionStorage or LocalStorage).

Tommy Elliott
  • 341
  • 1
  • 3
  • 9
0

You have a few different options but keep in mind that MVC is stateless. This means that MVC is ignorant of any information that was stored in your enums across page requests. Therefore, you need to pass parameters into your action method that receives the enum as a string, then parse the string back into an enum type. An example of how you can do this is here.

Or the copy+pasted code:

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);        
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))  
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}

There is also the fact that any page you want that enum to be accepted by will need to be passed the enum from the action method that generates the view. Then in the view itself, you have to accept and store the enum inside a hidden field.

aiwyn
  • 268
  • 2
  • 9