This is a migrated thread and some comments may be shown as answers.

Loading data on a button click

1 Answer 821 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michele
Top achievements
Rank 2
Michele asked on 19 Aug 2013, 02:54 PM
Hello,
I'm trying to develop a search form that will load datas from a stored procedure into a Kendo UI Grid item....

I've some problem passing back the Model to the Action Method in the controller...

Here's my view code

@model  DemoRicerca_Interno.Models.RicercaClienteObject
 
 
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 
 
 
 
<div>
    <div id="ricerca"></div>
 
    <div>Query : @Html.EditorFor(x => x.Query)</div>
    <div>Match Exact : @Html.EditorFor(x => x.MatchExact)</div>
    <div>AncheEstinti : @Html.EditorFor(x => x.AncheEstinti)</div>
    <button id="showGrid">Carica</button><br />
 
    <script type="text/javascript">
        $("#showGrid").click(function () {
            $("#grid").data("kendoGrid").dataSource.read();
        });
 
        
    </script>
 
 
    <div id="content">
        @(Html.Kendo()
            .Grid<DemoRicerca_Interno.Models.Rapporto>()
            .AutoBind(false)
            .Name("grid")
            .Columns(columns =>
                {
                    columns.Bound(x => x.IDInterno).Visible(false);
                    columns.Bound(x => x.IDRapporto);
                    columns.Bound(x => x.Descr);
                    columns.Bound(x => x.Filiale);
                    columns.Bound(x => x.RM);
                    columns.Bound(x => x.NDG);
                })
                .DataSource(ds => ds
                .Ajax()
                .Read(read =>
                    {
                        read.Action("GetClienti", "Ricerca");
                            //new DemoRicerca_Interno.Models.RicercaClienteObject { Query = Model.Query, MatchExact = Model.MatchExact, AncheEstinti = Model.AncheEstinti });
                    }
                    )
 
              )
 
            )
    </div>
 
 
</div>
The RicercaController

public
class RicercaController : Controller
   {
       public ActionResult Index()
       {
           ViewData.Model = new RicercaClienteObject();
 
           return View();
       }
 
       public ActionResult GetClienti([DataSourceRequest] DataSourceRequest request, RicercaClienteObject ricerca)
       {
           xxxEntities model = new xxxEntities();
 
           var res = model_SP_GET_INTESTATARIO(xxx, ricerca.Query, ricerca.MatchExact, xxx, DateTime.Now.AddDays(-1), 0, ricerca.AncheEstinti, null, null);
 
           List<Rapporto> lst = new List<Rapporto>();
 
           foreach (var item in res)
           {
               var rapporto = new Rapporto();
 
               rapporto.Descr = item.INTESTAZIONE.Trim();
               rapporto.Filiale = item.FILIALE.Trim();
               rapporto.IDRapporto = item.CODICE_RAPPORTO.Trim();
               rapporto.NDG = item.NDG.Trim();
               rapporto.RM = item.RM.Trim();
               rapporto.IDInterno = item.ID_RAPPORTO;
 
               lst.Add(rapporto);
           }
 
           var result = lst.ToDataSourceResult(request);
           return Json(result);
 
       }
 
   }
And the models

public class RicercaClienteObject
  {
     
      [DataType( System.ComponentModel.DataAnnotations.DataType.Text)]
      public string Query { get; set; }
      public int AncheEstinti { get; set; }
       
      public int MatchExact { get; set; }
  }
 
 public class Rapporto
  {
      public string IDRapporto { get; set; }
       
      public string Descr { get; set; }
       
      public int? IDInterno { get; set; }
       
      public string Filiale { get; set; }
       
      public string RM { get; set; }
       
      public string StatoRapporto { get; set; }
       
      public string NDG { get; set; }
       
      public string Servizio { get; set; }
       
      public int? IDCliente { get; set; }
  }
How do I pass back the Model without using a js function and create a javascript object?

Thanks

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 21 Aug 2013, 02:32 PM
Hello,

If the values are available when the view is being rendered then you can send them as route values with the request:

.Read(read =>
{
    read.Action("GetClienti", "Ricerca", new { Query = Model.Query, MatchExact = Model.MatchExact, AncheEstinti = Model.AncheEstinti });                       
})
If the values should be taken from the inputs on the page, then you should use the request Data function. Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Michele
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Share this question
or