Gurjeet Singh

Gurjeet Singh

  • NA
  • 90
  • 94.7k

Search records based on criteria (MVC)

Jan 28 2014 6:45 AM
I have three 3 Dropdownlist. When i select brand dropdownlist it's fill records in product dropdownlist as well as Table(it invoke Model.GetSaleRecords()) but i want to invoke Model.GetSaleRecords() method only when user click on Search Button.


Controller

 [HttpGet]
        public ActionResult postyourad()
        {
            return View(new masterList());
        }
        [HttpPost]
        public ActionResult postyourad(masterList ddlListPostData)
        {           
                return View(ddlListPostData);           
        }




Model Class

namespace ModelEdmx.Models
{
    public class masterList
    {

        [Required]
        public virtual string _model { get; set; }
        [Required]
        public virtual string _product { get; set; }
        [Required]
        public virtual string _brand { get; set; }

        public IList<SelectListItem> mastr_name { get; set; }
        ConnectionClass con_cs = new ConnectionClass();
        public SelectList getBrand()
        {
            IEnumerable<SelectListItem> stateList = (from s in con_cs.stock_entries                                                  
                                                     select new SelectListItem()
                                                     {
                                                         Text = s.brand,
                                                         Value = s.brand
                                                     }).Distinct().OrderBy(x => x).ToList<SelectListItem>();
            return new SelectList(stateList, "Value", "Text", _brand);
        }
        public SelectList getProduct()
        {
            IEnumerable<SelectListItem> ProductList = new List<SelectListItem>();
            if (!string.IsNullOrEmpty(_brand))
            {
                ProductList = (from s in con_cs.stock_entries
                               where s.brand == _brand
                               select new SelectListItem()
                               {
                                   Text = s.product,
                                   Value = s.product
                               }).Distinct().ToList<SelectListItem>();
            }
            return new SelectList(ProductList, "Value", "Text", _product);
        }       
        public SelectList getModel()
        {
            IEnumerable<SelectListItem> ModelList = new List<SelectListItem>();
            if (!string.IsNullOrEmpty(_brand) && !string.IsNullOrEmpty(_product))
            {
                ModelList = (from s in con_cs.stock_entries
                             orderby s.model
                             where s.brand == _brand && s.product== _product
                             select new SelectListItem()
                             {
                                 Text = s.model,
                                 Value = s.model
                             }).Distinct().ToList<SelectListItem>();
            }
            return new SelectList(ModelList, "Value", "Text", _model);
        }
        public List<Mydata> GetSaleRecords()
        {
            List<Mydata> query = new List<Mydata>();
            //var query="";
            if (!string.IsNullOrEmpty(_brand) && !string.IsNullOrEmpty(_product) && !string.IsNullOrEmpty(_model))           
            {

                query = (from pd in con_cs.SaleDtls_entries
                         join od in con_cs.SaleMstr_entries on pd.req_no equals od.req_no
                         where pd.brand == _brand && pd.product == _product && pd.model_no == _model
                         orderby od.Id
                         select new Mydata
                         {
                             Id = od.Id,
                             req_no = od.req_no,
                             product = pd.product,
                             brand = pd.brand,
                             price = pd.price,
                             Qty = pd.Qty,
                             bill_amt = od.bill_amt,
                         }).ToList();

               
            }
            else
            {
                if (!string.IsNullOrEmpty(_brand) && !string.IsNullOrEmpty(_product))
                {
                    query = (from pd in con_cs.SaleDtls_entries
                             join od in con_cs.SaleMstr_entries on pd.req_no equals od.req_no
                             where pd.brand == _brand && pd.product == _product
                             orderby od.Id
                             select new Mydata
                             {
                                 Id = od.Id,
                                 req_no = od.req_no,
                                 product = pd.product,
                                 brand = pd.brand,
                                 price = pd.price,
                                 Qty = pd.Qty,
                                 bill_amt = od.bill_amt,
                             }).ToList();
                }
                else
                {
                    if (!string.IsNullOrEmpty(_brand))
                    {
                        query = (from pd in con_cs.SaleDtls_entries
                                 join od in con_cs.SaleMstr_entries on pd.req_no equals od.req_no
                                 where pd.brand == _brand
                                 orderby od.Id
                                 select new Mydata
                                 {
                                     Id = od.Id,
                                     req_no = od.req_no,
                                     product = pd.product,
                                     brand = pd.brand,
                                     price = pd.price,
                                     Qty = pd.Qty,
                                     bill_amt = od.bill_amt,
                                 }).ToList();
                    }
                }
            }
            return query;
        }
    }
}

View

<form method="post" id="TheForm" action="">
 @using (Html.BeginForm("stok", "postyourad", FormMethod.Post, new { id = "TheForm" }))
 {
     @*@Html.DropDownListFor(x => x._brand, new MultiSelectList(Model.getBrand(), Model.getBrand()), "--Choose Your State--",*@
    @Html.DropDownListFor(x => x._brand, Model.getBrand(), "--Choose Your Brand--",
    new
    {
        onchange = "document.getElementById('TheForm').submit();"
    })
    @Html.DropDownListFor(x => x._product, Model.getProduct(), "--Choose Your  Product--",
    new
    {
        onchange = "document.getElementById('TheForm').submit();"
    })
    @Html.DropDownListFor(x => x._model, Model.getModel(), "--Choose Your Model--",
    new
    {
        onchange = "document.getElementById('TheForm').submit();"
    })
   
     <input type="submit" value="Search" />
   <table  id="tbl2" cellspacing="0" cellpadding="0" style="width: 692px;">
    @foreach (var list in Model.GetSaleRecords())
    {
        <tr>
            <td style="font-size: large; font-style: italic; font-weight: bold" class="c1">
              @list.Id
            </td>         
            <td class="c2" ><input value="@list.bill_amt" type="text" class="data1" />
            </td>
            <td class="c3">@list.brand
            </td>
            <td class="c4">@list.product
            </td>
            <td class="c5"><input value="@list.price" type="text" class="data1" />
            </td>
            <td class="c6">@list.Qty
            </td>
        </tr>
    }
     </table>
    
 }
</form>