Friday, 4 January 2019

DropDownLists

DropDownLists

@{
    ViewBag.Title = "Home Page";
    var clientlist = (SelectList)ViewBag.ClientType;
    var listSub = Dictionary.MSP_GetClientList().ToList(); 
}
-----------------------------------------------------------------------------------------------------------------

@Html.DropDownListFor(m => m.ABC.Type, clientlist as SelectList, "Select Client Type", new { @id="ddlClient"})
-----------------------------------------------------------------------------------------------------------------
@Html.DropDownListFor(m => m.ABC.Type, new SelectList(string.Empty,"Value","Text"), "Select Client Type", new { @id="ddlClient"})
-----------------------------------------------------------------------------------------------------------------

     public static class ClsDictionary
        {
            public static Dictionary<int,string> MSP_GetClientList()
            {
                var list = new Dictionary<int,string>();
                list.Add(1,"MSP Client A");
                list.Add(2,"MSP Client B");
                return list;
            }
        }
-----------------------------------------------------------------------------------------------------------------
public ActionResult Index()
        {
            ViewBag.DefaultDate=DateTime.Now.ToString("dd-MMM-yyyy");
            ViewBag.ClientType = new SelectList(ClsDictionary.MSP_GetClientList().OrderBy(x=>x.Value).ToList(),"Key","Value");

            return View();
        }
--------------------------------------------------------------------------------------------------------------------

  public static List<MSP_Client> GetSubList()
        {
            return new List<MSP_Client>() {
                new MSP_Client() { subId=9, parentId=2, Name="MSP Supply"},
                new MSP_Client() { subId=10, parentId=2, Name="MSP Bill"},
                new MSP_Client() { subId=11, parentId=2, Name="MSP Order"},
                new MSP_Client() { subId=13, parentId=2, Name="MSP Product"}};
        }
----------------------------------------------------------------------------------------------------------------------

 public static List<SelectListItem> GetProductById(string _id)
        {
            var result = (from f in db.MSP_Product
                          join c in db.MSP_Customer
                          on f.ProductCode equals c.CustProdCode
                          where c.CustProdCode = _id
                          select new { f.ProductCode, f.ProductName }).Distinct();

            var list = result.select(x => new SelectListItem() { Value = x.ProductCode.ToString(), Text = x.ProductName.ToString() }).ToList();
            list.Insert(0, new SelectListItem() { Value = "0", Text = "Please Select" });
            return list;
        }
-----------------------------------------------------------------------------------------------------------------------
 
 [HttpGet]
        public JsonResult SaleAction(int? value)
        {
            var data = db.ItemModel.where(m => m.code == value).select(s => new SelectListItem()
            {
                Value = s.ItemCode.ToString(),
                Text = s.ItemName.ToString()
            }).ToList();

            return Json(new { data, JsonRequestBehavior.AllowGet });
        }

<Script>
    $(document).ready(function () {
        var counter = 0;
        var InitValue = 0;
        var _val = $("txtCode").val();
        $.getJSON('Url.Action("SaleAction","Home/SaleAction")' + "?value=" + _val, function (result) {
            $("#ddl").html();
            var data = result.data;
            $("#ddl").append("<option value = \"\">Select Item</option>");
            for(var i=0;i<data.length;i++)
            {
                $("#ddl").append("<option value=" + data[i].Value + ">" + data[i].Text + "</option>");
                if (counter == 0)
                    InitValue = data[i].Value;
                counter = counter + 1;
            }
        });
    });
  </Script>