Ravi Pinjarkar

Ravi Pinjarkar

  • NA
  • 59
  • 4.1k

In mvc 3, how to get data (response) from controller to view using ajax post method

Jul 19 2012 2:28 AM
Hi, friends..

I created 1 REST WCF service (with  responseType xml), & uasing this service i trying to get data at client side (MVC Application), in mvc controller i geting data from service, & i stored this data in XmlElement.

& now i want this data in Telerik Gridview

In View I wrote ajax Post method to get response & store in grid view 

but i never understand how to return this xmlelement to ajax method and how to store in Grid View form ajax method

my written code at below..


View
****************************

this is my grid..

@(Html.Telerik().Grid("......").Name("Grid")
                          .Groupable()
                          .Filterable()
                          .Selectable()
                          .DataKeys(keys => keys.Add("CurrentDateTime"))                          
                          )


// here i passing date to get data from service

<script type ="text/javascript">
    function GetReport() {
        var CDateTime = new Date();
        CDateTime = $('#DATE').val();
        var getreportStr = new String();        
        getreportStr = "<DocumentElement><MEmpAttendance><CurrentDateTime>"+ CDateTime +"</CurrentDateTime></MEmpAttendance></DocumentElement>";
        AtdReport(getreportStr);
    }

//ajax method

    function AtdReport(getreportStr){
    var data=new String();
    data=getreportStr;
    $.ajax({
        type: 'POST',
        url: '@Url.Action("GetRprt")',
        data: { data: data },        
        async: false,
        success: function (response) {
            var GridView = $('#Grid').data('t-Grid');
            var jsonobj;
            var obj = JSON.parse(response);
            GridView.bindTo(obj);
            Window.location.reload(true);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            window.location.reload(true);
        }
    });

 }
</script>

Here i trying to return the data at ajax post

Controller
****************************

 [HttpPost]
        [ValidateInput(false)]
        public string GetRprt(string data)
        {
           
                HttpClient htp = new HttpClient("http://localhost:50361/MoAttndReport/date");                
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(htp.MakeRequest("POST", data).ResponseBody);               
                XmlElement xe = (XmlElement)xDoc.DocumentElement;                
                return xe.GetElementsByTagName("AtdReport")[0].InnerText;
          }



 please give some suggestions.... 
Thanks,



Answers (1)