Thursday 13 September 2012

Dropdown list in Mvc3

Dropdown list in asp.net Mvc 3  using Razor

1)Create Model   
    public class AddCatagory
    {       
        public List<SelectListItem> ParentList { get; set; }      
        [DisplayName("Parent")]
        public int parent { get; set; }
     }

2) Controller
        public ActionResult AddCatagory(AddCatagory AddModel)
        {
            AddModel.ParentList = parentListFu("VS");
            //AddModel.parent =0 ;
            return View(AddModel);// dont pass to forgot model in view
    }
      

// hardcoded function in controller

      private List<SelectListItem> parentListFu(string defaultValue)
        {
             List<SelectListItem> items = new List<SelectListItem>();
              items.Add(new SelectListItem { Text = "Nagaur", Value = "Na", Selected = (defaultValue == "na") });
         items.Add(new SelectListItem { Text = "Thalanju", Value = "Th", Selected = (defaultValue == "th") });     
           
            return items;
    }

3)View

@model Constellation.Models.BusinessLogic.AddCatagory // include model name view

for Dropdownlist
 
 @Html.DropDownListFor(x => x.parent, Model.ParentList, new { @class = "full-width" })

No comments:

Post a Comment