I have those two line of code in my view
var selectProduct = $('#select-product');
var hasProduct = '@ViewBag.productName';
if (hasProduct != "") {
alert(hasProduct);
selectProduct.val(hasProduct);
alert("Select Product = "+selectProduct.val());
}
and this is the HTML of select-product
<select id="select-product">
<option>Select a Product</option>
</select>
The first alert -> alert(hasProduct) appear with the correct value coming from the viewbag but the second alert appear always with value = select a product.
Any help ?
I also think, you have to add the list of options before selecting the new entry. Try this (it worked in my browser):