lundi 20 mars 2017

Common function in razor view to generate drop down in multiple view pages

I have page which contains multiple dropdowns with option like

<option>1<option>
<option>2<option>
<option>3<option>

and

<option>-5<option>
<option>-6<option>
<option>-7<option>

so i have created in a function to generate dropdown options in razor view.

 @functions {
        public List<SelectListItem> GenerateDropDown(int startvalue, int endValue)
        {
            var dropDownList = new List<SelectListItem>();
            for (int i = startvalue; i <= endValue; i++)
            {
                string val = i.ToString();
                dropDownList.Add(new SelectListItem { Text = val, Value = val });
            }
            return dropDownList;
        }
}

and using like this

 @Html.DropDownListFor(m => m.xyz, GenerateDropDown(1, 10))
 @Html.DropDownListFor(m => m.Abc, GenerateDropDown(2, 20))

this work fine but i want use the same function in multiple pages with out code duplication i tried using helper method but no use can any one suggest me how to centralize GenerateDropDown function.

Aucun commentaire:

Enregistrer un commentaire