MVC Enumerating a List of Enumerations in a Drop-down List Using Angular JS

I have an MVC enumeration list that I want to link in a drop-down menu using the angular method. How is this possible?

public enum DefultBookType : byte
{
  Not Direct= 0,
  Individual = 1
}

public ActionResult Application(){
  ViewBag.BT = Enum.GetNames(typeof(DefultBookType)).ToList();
  return View();
}

@{
 var lstBT=  @ViewBag.BT
}
<select ng-model="Btype" ng-options="ctr as ctr.Name for ctr in lstBT track by ctr.Id">
   <option value="">select</option>
</select>
+4
source share
2 answers

You can use Json.NET to convert a collection of objects into an array of objects, which must then be assigned to m

@{
 var lstBT = JsonConvert.SerializeObject(ViewBag.BT);
}

And then you need to assign lstBT to your controller model to use; you cannot directly refer to lstBT, since lstBT is the server line that will be displayed to the client as a client JavaScript array of JSON objects.

, ; , ViewBag ViewData.

+1

, , .

enum action controller.cs:

public enum DefultBookType : byte
{
    NotDirect = 0,
    Individual = 1
}

public ActionResult Application()
{
    ViewData["BT"] = JsonConvert.SerializeObject(Enum.GetValues(typeof(DefultBookType)), Formatting.Indented, new StringEnumConverter());
    return View();
}

.cshtml

<select ng-model="selected" ng-options="key as val for (key, val) in @ViewData["BT"]">
   <option value="">select</option>
</select>

:

enter image description here

+1

Source: https://habr.com/ru/post/1680490/


All Articles