使用Json

时间:2015-06-11 11:52:28

标签: c# jquery json

ActionResult Create():

ViewBag.Quantity = new SelectList(new List<SelectListItem>(), "Value", "Text");

查看:

@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);
    }

嗯,我只是想在更改产品后填写产品可用数量的下拉列表。我不知道出了什么问题......

我查看了身体反应,结果如下:

[{"Disabled":false,"Group":null,"Selected":false,"Text":"1","Value":"1"},{"Disabled":false,"Group":null,"Selected":false,"Text":"2","Value":"2"}]

正如我预期的那样,但它没有填写下拉列表。

回复标题:

Answer HTTP/1.1 304 Not Modified

我有很多具有相同语法的脚本,它们都运行良好。

3 个答案:

答案 0 :(得分:1)

首先,您需要按如下方式添加编码

    return Json(quantity,"text/json", JsonRequestBehavior.AllowGet);

并编辑您的jQuery代码

var $quantity = $('#Quantity');
function GetQuantity() {
    $.getJSON('/Sales/GetQuantity?Id=' + $products.val())
    .done(function (result) {
         var items=[];

         $quantity.empty()
            .append($('<option />', 
                   { value: '', 
                     text:     "select", 
                     selected: true, 
                     disabled: true }));

        $.each(result,function (index,item) {
           items.push('<option value="'+item.Value+'">'+item.Text+'</option>');
        });
         $quantity.html(items.join(''));
    })
    .fail(function (jqXHR) { console.log(jqXHR.responseText) });
};

答案 1 :(得分:0)

使用Json.Net

var response =  JsonConvert.DeserializeObject<Response>(json);
foreach(var item in response.datas)
{
    DropDownList1.Items.Add(new ListItem(item.Value, item.Key));
}

public class Response
{
    public bool state;
    public Dictionary<string, string> datas;
}

这可能也是一个问题:

public ActionResult GetQuantity(int Id)     {
        产品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()
        }
        ); <-----To what does this belong?
    }

    return Json(quantity, JsonRequestBehavior.AllowGet);
}

答案 2 :(得分:0)

如果您想更新此dropDown:

@Html.DropDownList("Quantity", null, htmlAttributes: new { @class = "form-control" })

id="Quantity"而非ProdQuantity

所以$('#ProdQuantity');应为$('#Quantity');

像这样更新下拉列表:

 var targetDropdown = $("#Quantity");


$.ajax({
        type: 'GET',
        url: '',
        contentType: 'application/json'
        success: function (response) {
            targetDropdown.empty();

    $.each(response, function (val, item) {
    targetDropdown.append($("<option></option>").val(item.Value).html(item.Text));
    });
    }