如何使用MVC控制器返回JSON [英] how to return JSON with MVC Controller

查看:101
本文介绍了如何使用MVC控制器返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.ajax调用我的控制器方法.我的控制器方法调用了返回字典的Web服务. 现在我需要返回此值并填充下拉列表.我正在尝试使用返回JSON,并且需要使用成功(响应)进行填充

I am calling my controller method using .ajax. my controller method call web service which returns dictionary. now i need to return this and populate dropdown list. i am trying with return JSON and need to populate using success (response)

我正在使用MVC 1.0

I am using MVC 1.0

        $.ajax(
            {
                url: 'LookupValue/',
                data: { 'sLookupIds': selectedtext },
                datatype: "json",
                traditional: true,
                success: function (data) {
                    alert(data.value);
                }
            });

提前谢谢.

推荐答案

在控制器中

    public JsonResult LookupValue(String sLookupIds)
    {

        SelectList olist = new SelectList(oDict, "key","value");

        return Json(olist);

  }

可见

        $.ajax(
            {
                url: 'LookupValue/',
                data: { 'sLookupIds': selectedtext },
                datatype: "json",
                traditional: true,
                success: function (data) {
                    $.each(data, function (index, val) {
                        $('#lookup')
                        .append($("<option></option>")
                        .attr("value", val.Value)
                        .text(val.Text));
                        //ddHTML = ddHTML + "<option  value='" + val.Value + "'>'" + val.Texts + "'</option>";
                    });
                }
            });

这篇关于如何使用MVC控制器返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆