Static Dropdown
In controller
List<SelectListItem> li
= new List<SelectListItem>();
            li.Add(new SelectListItem { Text
= "Select", Value = "0" });
            li.Add(new SelectListItem { Text
= "India", Value = "1" });
            li.Add(new SelectListItem { Text
= "Srilanka", Value = "2" });
            li.Add(new SelectListItem { Text
= "China", Value = "3" });
            li.Add(new SelectListItem { Text
= "Austrila", Value = "4" });
            li.Add(new SelectListItem { Text
= "USA", Value = "5" });
            li.Add(new SelectListItem { Text = "UK", Value = "6" });
  ViewData["country"] = li;
In View
@Html.DropDownList("Country", ViewData["country"] as List<SelectListItem>, "Show All")
In controller
List<SelectListItem> li = new List<SelectListItem>();
            li.Add(new SelectListItem() { Text
= "India", Value = "1" });
            li.Add(new SelectListItem() { Text
= "Srilanka", Value = "2" });
            li.Add(new SelectListItem() { Text
= "China", Value = "3" }); 
this.ViewBag.li = new SelectList(li, "Value", "Text");
In View
@model MvcApplication1.Models.ABCmodel
@Html.DropDownListFor(m => m.Name, (SelectList)ViewBag.li)
Dynamic Dropdown
In controller
  List<SelectListItem> li
= new List<SelectListItem>();
  var Model = (from c in db.Tables
                       select new ABCmodel
                      
 {
                          Id = c.Id,
                          Name = c.Name,
                    
   }) .ToList();
  for (int i = 0; i < Model.Count(); i++)
      {
        li.Add(new SelectListItem { Text
= Model[i].Name, Value = Model[i].Id.ToString() });
      }
ViewData["country"] = li;
In View
@Html.DropDownList("Country",
ViewData["country"] as List<SelectListItem>, "Show All")
In controller
List<SelectListItem> li = new List<SelectListItem>();
var Model = (from c in db.Tables
                       select new ABCmodel
                        
{
                             Id = c.Id,
                             Name = c. Name,
                         }).ToList();
for (int i = 0; i < Model.Count();
i++)
            {
                li.Add(new SelectListItem { Text
= Model[i].Name, Value = Model[i].Id.ToString() });
            }
this.ViewBag.li = new SelectList(li,
"Value", "Text");
In View
@Html.DropDownListFor(m => m.Name, (SelectList)ViewBag.li)
To Costomise the
Dropdown
<style>
    .button {
    background-color: #f9fafb;
    /* border: none; */
    border-radius: 4px;
    color: #1f1e1e;
    padding: 3px 20px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
}
</style>
@Html.DropDownList("Country",
ViewData["country"] as List<SelectListItem>, "Show
All", new { @class = "button" })
To Save selected value
of Dropdown
In controller
public ActionResult
DropDownList1(string Value)
        {
        // right
code for save here
            return View();
        }
In View
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input id="btnSave" type="button" value="Save" />
<script type="text/javascript">  
    debugger
    $("#btnSave").click(function () {
        debugger;
        $.ajax({
            url: '/AllControl/DropDownList1',
            data: {
                Value:
$('#Country').val(),
            },
            type: 'POST',
           
dataType: "json",
       });
    });  
</script>
No comments:
Post a Comment