0

Hi I am relatively new to MVC. I am building one application where I need to fill a drop down list. I have to fill values from Properties mentioned below,

public class Data
{
    public string to_node_id { get; set; }
    public Datacenter datacenter { get; set; }
    public string data_type { get; set; }
    public string msg_id { get; set; }
    public string from_type { get; set; }
    public string from_node_id { get; set; }
}

public class Message
{
    public Data data { get; set; }
} 

public class Datacenter
{
    public string order_id { get; set; }
    public string lastmodify { get; set; }
    public string type { get; set; }
    public string order_type { get; set; }

}

Please can anyone help.

I am using razor syntax.

Dezler
  • 47
  • 5

5 Answers5

0

It's like :

@{
       List<SelectListItem> listItems= new List<SelectListItem>();
       listItems.Add(new SelectListItem
            {
              Text = "Exemplo1",
              Value = "Exemplo1"
            });
       listItems.Add(new SelectListItem
            {
                Text = "Exemplo2",
                Value = "Exemplo2",
                Selected = true
            });
       listItems.Add(new SelectListItem
            {
                Text = "Exemplo3",
                Value = "Exemplo3"
            });
    }

    @Html.DropDownListFor(model => model.order, listItems, "-- Select Order--")
Rai Vu
  • 1,595
  • 1
  • 20
  • 30
0

The DropDownListFor (or DropDownList) with SelectList is the way to go.

I would suggest to create the selectList in the action in the controller (instead of the view) and send it to the View by the ViewBag (or ViewData) as it is how Microsoft does it when Visual Studio generates a CRUD form.

Community
  • 1
  • 1
Giu
  • 1,832
  • 2
  • 16
  • 31
0

Use viewbag to pass data as below, You can write in controller For static list

var genderlist = new List<SelectListItem>
{
   new SelectListItem { code = "M", name = "Male" },
   new SelectListItem { code = "F", name = "Female" }                
};
viewbag.genderlist=genderlist;

same as above you can pass dynamic listing too

Now in your view

@Html.DropDownListFor(model => model.gender, new SelectList(ViewBag.genderlist, "code", "name"));

Thats it!

Divyek
  • 155
  • 2
  • 12
  • Thanks for the reply. But I don't want to provide any values. I just want to show all the properties in the drop down. – Dezler Dec 08 '16 at 10:44
  • Do you need combination of values in dropdown like ? Please explain your question. – Divyek Dec 08 '16 at 14:15
0

I wrote about dropdown lists in here: http://davidsonsousa.net/en/post/the-best-way-to-use-dropdownlistfor-with-aspnet-mvc

But in general I create my dropdown lists by having:

  1. Editor Templates - I can control the look/feel or customize using javascript
  2. A helper class - I can control what data is going to be loaded into the dropdown list
  3. A ViewModel to transfer the data from the Controller to the View

The most important here is the helper class. And why is that? For 2 reasons:

  1. Because you will need to load the values from somewhere: DB, text file, web service or even hard coded
  2. You will not have only one dropdown in your project
Davidson Sousa
  • 1,353
  • 1
  • 14
  • 34
0

Use the jQuery function for this dropdown list

<select id="dropDown"><select>
var urlpath = '@Url.Action("LoadModule", "MenuOperation")';

$.ajax({
    url: urlpath,
    dataType: 'json',
    async: false,
    type: "Get",
    success: function (data) {
        $('#dropDown').empty();
        $('#dropDown').append("<option value='0'>--Select--</option>");
        for (var i = 0; i < data.length; i++) {

            $("#dropDown").append($("<option></option>").val(data[i].Value).html(data[i].Name));
        }
    }
});
Pang
  • 9,564
  • 146
  • 81
  • 122