Tuesday, February 11, 2014

How To Create Static DropdownList in MVC 5

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

@Html.DropDownListFor(model => model.tipo, listItems, "-- Select Status --")
 
 
 
Another Example:
CREATE 
 
 <div class="form-group">
                @Html.LabelFor(model => model.RattioType, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                @{
                 List<SelectListItem> listItems = new List<SelectListItem>();
                
                 listItems.Add(new SelectListItem
                 {
                 Text = "Percent",
                 Value = "0",
                  Selected = true
                  });
                 
                  listItems.Add(new SelectListItem
                {
                 Text = "Fixed",
                 Value = "1"
                
                }); 
       
                    }

                    @Html.DropDownList("RattioType", listItems, "Percent")
                    @Html.ValidationMessageFor(model => model.RattioType)
                </div>
            </div> 
 
EDIT
 <div class="form-group">
                @Html.LabelFor(model => model.RattioType, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @{
                     List<SelectListItem> listItems = new List<SelectListItem>();

                      listItems.Add(new SelectListItem
                    {
                      Text = "Percent",
                      Value = "0",
                      Selected = true
                    });

                  listItems.Add(new SelectListItem
                 {
                 Text = "Fixed",
                 Value = "1"

                 });

                    }

                    @Html.DropDownList("RattioType", listItems)
                    @Html.ValidationMessageFor(model => model.RattioType)
                </div>
            </div> 

No comments:

Post a Comment