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

how to rebind the telerikgrid

2 Answers 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
vinkesh
Top achievements
Rank 1
vinkesh asked on 14 Jul 2011, 07:45 AM
hello telerik,
 I want rebind the grid on the click of the submit button.
I'll be using telerik grid that has to be filter according to the value selected  in the datepickers(date).
Please provide  me solution if there is any.
code will like this:

  function abc() {
         alert("rr")
         var date1 = "'"+$("#datepicker1").data("tDatePicker").value()+"'"; 


         alert(date1);
         var date2 = "'"+$("#datepicker2").data("tDatePicker").value()+"'";
         alert(date2);
       
         var url = "Home/Select";


         alert(url);
         $.getJSON(url, { datepicker1: date1, datepicker2: date2 },
                       function (data) {
                           var abc = data;
                           date1: $.telerik.formatString("{0:d}", date1);
                           alert("ooo");
                           date2: $.telerik.formatString("{0:d}", date2);


                           if (abc != "") {
                               var v = abc;
                               alert(v);
                           }


                          
                       });


     }


   
    </script>








    @( 
    Html.Telerik()
    .ScriptRegistrar()
    .OnDocumentReady("attachFilter()")
    )
    




@(Html.Telerik().DatePicker()
    .Name("datepicker1")
    .Min(new DateTime(2010, 1, 1))
    .Max(new DateTime(2012, 12, 31))
    .ShowButton(true)
    )


    @(Html.Telerik().DatePicker()
    .Name("datepicker2")
    .Min(new DateTime(2010, 1, 1))
    .Max(new DateTime(2012, 12, 31))
    .ShowButton(true)
  
    )


    @*<a class="t-button" id="submit-btn" >Submit</a>*@
@*<button id="submit-btn" type="submit" class="t-button t-state-default" onclick="attachFilter();">Submit</button>
*@
    <input id="Submit1" type="submit" value="submit" onclick="abc();"/>




 @(Html.Telerik().Grid<TelerikMvcApplication3.Models.tb_TestMvc>()
.BindTo((List<TelerikMvcApplication3.Models.tb_TestMvc>)ViewData["db_Test"])
        .Name("Grid")




         .DataKeys(keys =>
         {
             keys.Add(p => p.Id);
         })
                  .DataBinding(dataBinding => dataBinding.Server()
                   .Update("UpdateTest", "HOME")
                           .Insert("InsertTest", "HOME")
                                   .Delete("DeleteTest", "HOME")
                                             //.Select("Select", "HOME")
                           )


              .Columns(columns =>
              {
                  columns.Bound(p => p.Id).Width(210).ReadOnly();
                  columns.Bound(p => p.ContactTitle).Width(130).Format("{0:c}");
                  columns.Bound(p => p.ContactName).Width(130).Format("{0:N}");
                  columns.Bound(p => p.Country).Width(130).Format("{0:d}");
                  columns.Bound(p => p.Product).Width(130).Format("{0:d}");
                  columns.Bound(p => p.ProDetail).Width(130).Format("{0:d}");
                  columns.Bound(p => p.ProPrice).Width(130).Format("{0:d}");
                  columns.Bound(p => p.OrderDate).Width(130).Format("{0:d}");
                  columns.Bound(p => p.country_Id).Width(130).Format("{0:d}");
                  columns.Bound(p => p.Id)
                   .ClientTemplate("<img width='100' height='110' alt='<#=Id #>' src='"
                    + Url.Content("../../Images/")
                     + "<#=Id #>.jpg' />")
                     .Title("Picture");




                  //columns.Bound(p => p.country).ClientTemplate(
                  //                       Html.Telerik().DropDownList()
                  //                           .Name("ddlcnt")
                  //                           .BindTo(new SelectList(UserController.GetRoles()))
                  //                           .ToHtmlString()




                  //).Title("CountryName");








                  //columns.Bound(p => p.Country);


                  columns.Command(commands =>
                      {
                          commands.Edit();
                          commands.Delete();
                      });
              })


                           .ToolBar(t =>
                           {
                               t.Insert();
                           })


                 .Sortable()
                .Filterable()
                 .Pageable(paging => paging.PageSize(5))
                 
                   )


In  this will not able to show the images in the telerik grid column. please suggest me what should  i do to show the images.

public class HomeController : Controller
    {
       public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";


            var tme = new db_TestEntities1();
            ViewData["db_Test"] = tme.tb_TestMvc.ToList();
            return View(GetOrders());
        }


           public ActionResult About()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateTest(int Id)
        {
            var tst = new test
            {
                Id = Id,
                
            };
            if (TryUpdateModel(tst))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd1 = new SqlCommand();
                cmd1.CommandText = "Update tb_TestMvc set Contacttitle=@Contacttitle,ContactName=@ContactName,Country=@country where Id=@Id";
                cmd1.Connection = con;
                cmd1.Parameters.AddWithValue("@Id", Id);
                cmd1.Parameters.AddWithValue("@Contacttitle", tst.Contacttitle);
              cmd1.Parameters.AddWithValue("@ContactName", tst.ContactName) ;
              cmd1.Parameters.AddWithValue("@Country", tst.Country);
                cmd1.ExecuteNonQuery();
                con.Close();
            }


            var tme = new db_TestEntities1 ();
            ViewData["tb_TestMvc"] = tme.tb_TestMvc.ToList();
            return RedirectToAction("Index", "Home");
           // return View("Index");
           
        }




        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertTest(int Id, string Contacttitle)
        {
            var tst = new test
            {
                Id = Id,


            };
            if (TryUpdateModel(tst))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd1 = new SqlCommand();
                cmd1.CommandText = "Insert into tb_TestMvc (Id,Contacttitle,ContactName,Country) values (@Id,@Contacttitle,@ContactName,@Country)";
                cmd1.Connection = con;
                cmd1.Parameters.AddWithValue("@Id", Id);
                cmd1.Parameters.AddWithValue("@Contacttitle", Contacttitle);
                cmd1.Parameters.AddWithValue("@ContactName", tst.ContactName) ;
                cmd1.Parameters.AddWithValue("@Country", tst.Country);
                cmd1.ExecuteNonQuery();
                con.Close();
            }


            var tme = new db_TestEntities1();
            ViewData["tb_TestMvc"] = tme.tb_TestMvc.ToList();
            return RedirectToAction("Index", "Home");
            // return View("Index");


        }
         [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteTest(int Id, string Contacttitle)
        {
            var tst = new test
            {
                Id = Id,


            };
            if (TryUpdateModel(tst))
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd1 = new SqlCommand();
                cmd1.CommandText = "Delete from tb_TestMvc where Id=@Id";
                cmd1.Connection = con;
                cmd1.Parameters.AddWithValue("@Id", Id);
               
                cmd1.ExecuteNonQuery();
                con.Close();
            }


            var tme = new db_TestEntities1();
            ViewData["tb_TestMvc"] = tme.tb_TestMvc.ToList();
            return RedirectToAction("Index", "Home");
            // return View("Index");


        }
        [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult _TemplatesClientSide(int Id)
         {
             var tst = new test
            {
                Id = Id,


            };
             if (TryUpdateModel(tst))
             {
                 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                 if (con.State == ConnectionState.Closed)
                 {
                     con.Open();
                 }
                 SqlCommand cmd1 = new SqlCommand();
                 cmd1.CommandText = "Select country_img from tb_Country where country_Id=@country_Id";
                 cmd1.Connection = con;
                 cmd1.Parameters.AddWithValue("@country_Id", tst.country_Id);
                 cmd1.Parameters.AddWithValue("@country_img", tst.country_img);
                 cmd1.ExecuteNonQuery();
                 con.Close();
             }
             var tme = new db_TestEntities1();
             ViewData["tb_TestMvc"] = tme.tb_Country.ToList();
             return RedirectToAction("Index", "Home");
             }
         [GridAction]
       






        public ActionResult Select(DateTime? date1, DateTime? date2)
        {
            var orders = GetOrders();
            if (date1.HasValue)
            {
                orders = orders.Where(o => o.OrderDate >= date1);
            }


            if (date2.HasValue)
            {
                orders = orders.Where(o => o.OrderDate <= date2);
            }


            return Json(new GridModel(orders));
        }


         


        IEnumerable<test> GetOrders()
        {
            return new db_TestEntities1().tb_TestMvc.Select(o=> new test
                                                              {
                                                                  Id = o.Id,
                                                                  Contacttitle = o.ContactTitle,
                                                                  ContactName = o.ContactName,
                                                                  State=o.State,
                                                                  Product=o.Product,
                                                                  ProDetail=o.ProDetail,
                                                                  ProPrice=Convert.ToInt32(o.ProPrice),
                                                                  OrderDate=Convert.ToDateTime(o.OrderDate),
                                                                  country_Id=Convert.ToInt32(o.country_Id),
                                                                  country_img=o.country_img,
                                                              });
        }
    }
         }
   

in this i m not able to access the date  in the same format as on the index .cshtml. i can access them  in the string format but then  i am unable to  convert them again in the datetime format.It will giving me error that it is not in the right format.




2 Answers, 1 is accepted

Sort by
0
James
Top achievements
Rank 1
answered on 07 Jan 2012, 10:35 PM
I have the same question. How do I rebind my grid on a button click? 
0
Petur Subev
Telerik team
answered on 09 Jan 2012, 11:35 AM
Hello James,

Basically to achieve it you need to call the submitChanges method of the grid when the button outside the Grid is clicked.
To do so you can use the following approach:
Copy Code
<button class='t-button' onclick='performSubmit()'>Submit changes</button>
 
<script type="text/javascript">
    function performSubmit() {
        var grid = $('#YourGirdName').data('tGrid');
        grid.submitChanges();
    };
</script>

Also if you want to send additional values to the action method handling the update request please check this post.
I hope this helps. 


Greetings,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
vinkesh
Top achievements
Rank 1
Answers by
James
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or