Tuesday, September 18, 2012

Jquery Create a Drop Down


                   How to create a Drop down using Jquey & Json  in 


Call a Server Side Function using jquery


$(document).ready(function() {



 $.getJSON("/Product/GetProduct/" + $("#Pc_Id").val(),  function (data) {
            var items = "<option selected></option>";
            $.each(data, function (i, item) {
                items += "<option value='" + item.ID + "'>" + item.Val + "</option>";
            });
            $("#Product").html(items);
        });


});

Add a Drop down
<select id="product" name="product"><option value=""></select>








Controllers Class


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Product.Models;
using System.IO;
using System.Data.Common;



namespace Product.Controllers{
    public class ProductController : Controller{

        private astrocampEntities1 db = new astrocampEntities1();      //Database from Entities


  public ActionResult GetProduct(string id = "0"){  
            int Cat_ID=Convert.ToInt32(id);
            var items = from i in db.tbl_Products
                        where (i.Pc_Id == Cat_ID)
                        orderby i.Pr_Name,i.Pr_Id
                        select new
                        {
                ID=i.Pr_Id,
                Val=i.Pr_Name                
            };
            return this.Json(items, JsonRequestBehavior.AllowGet);
    }

}

No comments:

Post a Comment