This question is locked. New answers and comments are not allowed.
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.
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.