MVC - comparison of 2 viewdata

247 Views Asked by At

I have 2 viewdata, ViewData["product"] & ViewData["ProductRetailPriceList"] values assigned in controller reading from database as follow:

 var product = db.ProductTbls.ToList();
   List<SelectListItem> productlist = new List<SelectListItem>();
   List<SelectListItem> ProductRetailPriceList = new List<SelectListItem>();

        foreach(ProductTbl item in product)
        {
            productlist.Add(new SelectListItem
                {
                    Text = item.ProductName,
                    Value = item.ProductID.ToString()
                }
                );

            ProductRetailPriceList.Add(new SelectListItem
                {
                    Text = item.RetailUnitRentPrice.ToString(),
                    Value = item.ProductID.ToString()

                }); 
ViewData["product"]  = productlist;
ViewData["ProductRetailPriceList"] = ProductRetailPriceList;

I assign the value of the ViewData["product"] to a dropdownlist in the view as follow:

@Html.DropDownListFor(model => model.ProductNamevm, (IEnumerable<SelectListItem>)ViewData["product"], new { id = "productname" })

For the price textbox I created it as follow:

@Html.TextBoxFor(model => model.ProductPrice, new { id = "productprice" })

I want to write a javascript that will allow me to change the value of product price textbox when I change the value the product name dropdownlist what I got so far is the following:

 $("#productname").change(function ()
                {
                    getprice();
                })



                function getprice()
                {
                    var productname = $("#productname").val();
                    var clienttype = $("#clienttype").val();


                    if (clienttype  == "Retail")
                    {

                        foreach (productname == ViewData["ProductRetailPriceList"])
                        {
                            var x = ViewData["ProductRetailPriceList"];
                            $("#productprice").val(x);
                        }

                    }   
                }

FYI ClientType is a dropdownlist I dont have issues with. When I inspect in the browser the code doesnt go inside the foreach. can someone guide me how to compare ViewData["ProductRetailPriceList"] to the selected ViewData["product"] so I can assign it to the price textbox. Thanks.

0

There are 0 best solutions below