Wednesday, 28 November 2012

DataBinding in asp.net mvc?


DataBinding in ASP.NET MVC :

              Yes, In asp.net à all standard bidning controls are requires ListItem collection.EX: dropdown, radaiobutton,listbox..ect

Binding Items to DropDownList asp.net mvc

1st way

  List<string> languages = new List<string>();
            languages.Add("Chsarp");
            languages.Add("VisualBasic");
            languages.Add("FSharp");
            ViewData["languges"] = new SelectList(languages);  // as IEnumerable
            return View();



@Html.DropDownList("languges", "--select one--")  ----------loosely copuled

------------------------------------------------------------------------------------
2nd way


           SelectListItem list1 = new SelectListItem();
            list1.Text = "ONE";
            list1.Value = "one";

            SelectListItem list2 = new SelectListItem();
            list2.Text = "TWO";
            list2.Value = "two";

            List<SelectListItem> slist = new List<SelectListItem>();
            slist.Add(list1);
            slist.Add(list2);

            ViewData["key"] = slist;

            return View();

@Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>)ViewData["key"])
// as IEnumerable

3rd way

            SelectListItem list1 = new SelectListItem();
            list1.Text = "ONE";
            list1.Value = "one";

            SelectListItem list2 = new SelectListItem();
            list2.Text = "TWO";
            list2.Value = "two";

            List<SelectListItem> slist = new List<SelectListItem>();
            slist.Add(list1);
            slist.Add(list2);

          
            return View(slist);

@model IEnumerable<SelectListItem>               // as IEnumerable

@Html.DropDownList("strings",Model)

------------------------------------------------------------------------------


IQueryable<SelectListItem> query = conobj.emps.Select(t => new SelectListItem { Text = t.EName, Value = t.ELocation, Selected = t.EId.Equals(2) });

            IEnumerable<SelectListItem> ier = query.AsEnumerable<SelectListItem>();

            return View(ier);
 
@model IEnumerable<SelectListItem>

@Html.DropDownList("strings",Model)

--------------------------------------------------------------------------------

   IQueryable<SelectListItem> d = conobj.emps.Select(t => new SelectListItem { Text = t.ELocation, Value = t.EName });
            IEnumerable<SelectListItem> j = d.AsEnumerable<SelectListItem>();
            return View(j);



@model IEnumerable<SelectListItem>

@Html.DropDownList("ssl",Model,"--select--")
-----------------------------------------------------------------------------------

   List<SelectListItem> items = new List<SelectListItem>();
   items.Add(new SelectListItem() { Text = "Test1", Value = "1", Selected = false });
   items.Add(new SelectListItem() { Text = "Test8", Value = "8", Selected = true });
   items.Add(new SelectListItem() { Text = "Test3", Value = "3", Selected = false });
   items.Add(new SelectListItem() { Text = "Test5", Value = "5", Selected = false });


  SelectList selectList = new SelectList(items, "Value", "Text");


NOTE:

any way but we must convert them as IEnumerable<SelectListItem> is must.




No comments:

Post a Comment