Monday, 16 October 2017

redirect grid data to another page in mvc using kendo ui

redirect grid data to another page in kendo mvc  using ajax post

in view
when we click on edit button it will go to another view with its row id.
columns.Command(command => command.Custom("Edit").Click("editDetails")).Width(120);
<script type="text/javascript">

    //......................<Edit>............
    function editDetails(e) {
        debugger
        var dataItem = this.dataItem($(e.target).closest("tr"));
        var id = dataItem.CompetitorId;
        location.href = "@Url.Action("", "Controllername/viewname")?Id="+id
    }
</script>
Another view page
In view
<script>
    $(document).ready(function () {
        debugger
        var baseUrl = (window.location).href; // You can also use document.URL
        var koopId = baseUrl.substring(baseUrl.lastIndexOf('=') + 1);
     
        $.ajax({
            //type: "GET",
            type: 'POST',
            url: "/Crm_Competitor/GetDetailById",
            data: { id: koopId },
            //   contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                debugger;
                document.getElementById("CompetitorId").value = data.CompetitorId;
            },

        });
      
    });

</script>

In controller

public ActionResult GetDetailById([DataSourceRequest]DataSourceRequest req,int id)
        {
            try
            {
                Detail = new CompetitorDetail();
                var request = new RestRequest("api/CompetitorDetail/" + id, Method.GET) { RequestFormat = DataFormat.Json };

                var response = _client.Execute<List<CompetitorDetail>>(request);

                if (response.Data == null)
                    throw new Exception(response.ErrorMessage);

                return Json(response.Data.ToList()[0]);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }

No comments:

Post a Comment