Kendo Grid没有读取json数据?

时间:2015-05-27 08:44:56

标签: c# json kendo-ui json.net kendo-grid

这是我的javascript代码,我用于在剑道上使用crud记住我正在使用api调用并且在测试中我必须只使用json数据

function login_user() {

{
    $this->load->model('admin_model');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('login');
        } else {
            $data = array(
            'email' => $this->input->post('email'),
            'password' => $this->input->post('password')
            );
            $result = $this->admin_model->validate_user($data);
            if($result == TRUE){
                    $sess_array = array(
                    'email' => $this->input->post('email')
                );
                // Add user data in session
                    $this->session->set_userdata('logged_in', $sess_array);
                    $result = $this->admin_model->read_user_information($sess_array);
                if($result != false){



                $data = array(
                        'fname' =>$result[0]->fname,
                        // 'user_name' =>$result[0]->user_name,
                        'email' =>$result[0]->email,
                        'password' =>$result[0]->password,
                        'admin_flag'=>$result[0]->admin_flag
                    );
                        //if(num_rows()->admin_flag == 1){
                        $this->load->view('success',$data);
                    //  }
                    //  else{
                        //  $this->load->view('success2',$data);
                        //}


                }
            }
            else
                {
                    $data = array(
                        'error_message' => 'Invalid Username or Password'
                    );
                        $this->load->view('login', $data);
                }
        }
    }
}

这是我的服务器端代码返回json数据,我已经测试过并且它成功返回了json数据,我需要它,因为它正是

  document.onreadystatechange = function () {



var viewModel = kendo.observable({

    products: new kendo.data.DataSource({


        transport: {

            read: {
              type: "GET",
                url: "/api/Companies/GetAllCompanies2",
                dataType: "json"
            },

            create: {

                 type: "PUT",
                    url: "/api/Companies/UpdateDefCompny",
                  contentType: "application/json; charset=utf-8",
                    dataType: "json",
                async: false

            },
            update: {
                url:"/api/Companies/SaveDefCompny",
                async: false,
                contentType: "application/json",
                dataType: "json",
                type: "POST"
                 // here you need correct api url

            },
            destroy: {
                url: "/api/Companies/Delete", // here you need correct api url
                dataType: "json"
            },


            parameterMap: function (data, operation) {
                if (operation !== "read" && data) {
                    return JSON.stringify(data.models[0]);
                }
            }
        },
        serverPaging: true,
        serverFiltering: true,
        pageSize: 10,
        schema: {
            //data:"Data",
            total: "Count",

            model: {
                id: "Id",
                fields: {
                    Id: { type: "int" },
                    CurrentCurrencyCode: { editable: true, type: "int" },
                    ShortName: { editable: true, type: "string" },
                    FullName: { editable: true, type: "string" },
                    ContactPerson: { editable: true, type: "string" },
                    Address1: { editable: true, type: "string" },
                    CompanyCity: { editable: true, type: "string" },
                    CompanyState: { editable: true, type: "string" },
                    CompanyCountry: { editable: true, type: "string" },
                    ZipPostCode: { editable: true, type: "string" },
                    TelArea: { editable: true, type: "string" }

                }
            }
        },
        batch: true,


    }) 


});


   kendo.bind(document.getElementById("example"), viewModel);

  }

网格代码:

   [HttpGet]
    public string GetAllCompanies2()
    {
        List<object> myCo = DefCompany.AllDefCompany2;


        object json = JsonConvert.SerializeObject(myCo);
        return json.ToString();

    }

这里的问题是,为什么kendo网格没有填充json数据,它只在我反序列化或返回对象时才会弹出,但我必须用json填充它?

2 个答案:

答案 0 :(得分:1)

在global.aspx.cs文件中

     using System.Data.Entity;

  namespace RpManticSolAPI
  {
  public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);          
    }
}
   }

答案 1 :(得分:0)

而不是:

[HttpGet]
public string GetAllCompanies2()
{
    List<object> myCo = DefCompany.AllDefCompany2;


    object json = JsonConvert.SerializeObject(myCo);
    return json.ToString();

}

这样做:

[HttpGet]
public List<object> GetAllCompanies2()
{
    List<object> myCo = DefCompany.AllDefCompany2;
    return myCo;
}

您的数据将以纯json的形式返回,无需解析。