ActionResult Create():
ViewBag.Quantity = new SelectList(new List<SelectListItem>(), "Value", "Text");
View:
@Html.DropDownList("Quantity", null, htmlAttributes: new { @class = "form-control" })
jQuery:
var $quantity = $('#ProdQuantity');
function GetQuantity() {
$.getJSON('/Sales/GetQuantity?Id=' + $products.val())
.done(function (result) {
$quantity.empty().append($('<option />', { value: '', text: $select, selected: true, disabled: true }));
$(result).each(function () {
$quantity.append(
$('<option />', {
value: this.Value
}).html(this.Text))
});
})
.fail(function (jqXHR) { console.log(jqXHR.responseText) });
};
ActionResult GetQuantity():
public ActionResult GetQuantity(int Id)
{
Product product = db.Products.Find(Id);
var quantity = new List<SelectListItem>();
for (var i = 1; i <= product.Quantity; i++)
{
quantity.Add(new SelectListItem()
{
Text = i.ToString(),
Value = i.ToString()
}
);
}
return Json(quantity, JsonRequestBehavior.AllowGet);
}
Well I'm just trying to fill a dropdownlist of available quantities of a product, after change the product. I don't know what's wrong..
I looked at the body response, here is the result:
[{"Disabled":false,"Group":null,"Selected":false,"Text":"1","Value":"1"},{"Disabled":false,"Group":null,"Selected":false,"Text":"2","Value":"2"}]
As I expected, but it isn't filling the dropdown.
The response header:
Answer HTTP/1.1 304 Not Modified
I have lots of scripts with the same syntax, and they all are working fine.