Wednesday, March 19, 2014

Static DropdownList in MVC 5 Razor

Static List of Items in a DropdownList using MVC 5 

Create.cshtml

 <div class="form-group">

                @Html.LabelFor(model => model.ProjectStatus, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                @{
                 List<SelectListItem> lstItems = new List<SelectListItem>();

                lstItems.Add(new SelectListItem
           
                {
                  Text = "Upcoming",
                  Value = "1",
                 //Selected=true
                });

               lstItems.Add(new SelectListItem
               {

               Text = "Ongoing",
               Value = "2"
       

               });
              lstItems.Add(new SelectListItem
                {

                Text = "Ready",
                  Value = "3"
         

                 });


                    }
                    @Html.DropDownList("ProjectStatus", lstItems)
                    @Html.ValidationMessageFor(model => model.
ProjectStatus)
                </div>
            </div>




To add CSS Class 'dropdownList'  : 

@Html.DropDownList("ProjectStatus",listItems,new{@class="dropdownList"}) 

Index.cshtml 

<td>
    @{


  @(item.ProjectStatus.ToString()=="1"?"Upcoming":

(item.ProjectStatus.ToString()=="2"?"Ongoing":"Ready"))                
      }
  </td>

To Get:

 

 

No comments:

Post a Comment