MVC - comparison of 2 viewdata


MVC - comparison of 2 viewdata



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


ViewData["product"]


ViewData["ProductRetailPriceList"] values


database


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:


ViewData["product"]


dropdownlist


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


dropdownlist


$("#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.


ClientType


dropdownlist





First ViewData["ProductRetailPriceList"] should not be a IEnumerable<SelectListItem> (you are not using it in a dropdownlist - it should just be a collection of the ProductID and RetailUnitRentPrice). Then you need to convert it to a javascript array - refer this answer and find the price based on the ProductID
– Stephen Muecke
2 days ago


ViewData["ProductRetailPriceList"]


IEnumerable<SelectListItem>


ProductID


RetailUnitRentPrice


ProductID





And why are you assigning it to a textbox? - it should be display only (user should not be able to edit it)
– Stephen Muecke
2 days ago





ok got your point on IEnumerable<SelectListItem> just to clarify I will create a ViewModel that will include ProductID and RetailUnitRentPriceand then assign it to array in javascript
– O.A.K
2 days ago





BTW it was a client requirement to allow the user to change the price on the spot for certain customer so I kept enabled and when the user submit the form the data is inerted to a different table
– O.A.K
2 days ago










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV