Gurjeet Singh

Gurjeet Singh

  • NA
  • 90
  • 94.7k

How to Fill Records in Table in mvc

Feb 3 2014 2:37 AM












When user click on submit button then only records get fill into table.

Here is My Code:

  Contoller:
    [HttpGet]

        public ActionResult postyourad()

        {

            return View(new masterList());

        }

        [HttpPost]

        public ActionResult postyourad(masterList ddlListPostData)

        {

            return View(ddlListPostData);

        }



View :

@model withoutModelEdmx.Models.masterList

 

@{

    ViewBag.Title = "dropdownlist bind demo with model"

}

@using (Html.BeginForm("postyourad", "stok", FormMethod.Post, new { id = "TheForm" }))

 {

    @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="submit"  />

   <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> 

 }



Model:

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> BrandList = (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(BrandList, "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>();         

            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;

        }

    }







Answers (1)