Hi Experts & friends,
This is my first project with Telerik MVC. I have searched the Examples, Demos, blogs and this forum before posting here. I am in desparate need of a solution. Please help.
I have a view model which has a List object. This List object is used to bind to the Telerik grid. In the Telerik grid, I have used Template Column to display the checkbox. The user can change the checkbox value for one or more rows. Based on the selection, the controller has to do some operation. The objective is to find the rows where the checkbox value is modified.
Problem:
1. After Save button click (Post operation), the View Model parameter shows NULL for the CustomerList Object.
2. Alternatively, as explained in the Demos, I have included a variable int[ ] IsSelected = new int[ ] { }; This array gets me all the records where checkbox is selected. But, I need only the records where the checkbox value is changed. Another problem in this approach, which I don's mind though is, it does not get me the record if the checkbox is checked but disabled.
http://demos.telerik.com/aspnet-mvc/grid/checkboxesserverside
3. When I was not using the Telerik grid, I used a alternate approach which was explained here. i.e. The name attribute is set in a way where it will render like name="CustomerList[0].ID"
http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx
Model:
Class CustomerViewModel { public string CustomProperty1 {get; set;} public string CustomProperty2 {get; set;} public List<Customer> CustomerList {get; set;} } Class Customer { public int ID {get; set;} public string Name {get; set;} public bool IsBlacklisted {get; set;} public bool IsSelected {get; set;} }
Controller:
public ActionResult Index() { CustomerViewModel basket = PopulateViewModel(); // viewModel built here return View(basket); // viewModel is bind to the grid} [HttpPost] public ActionResult Save(CustomerViewModel viewModel, int[] IsSelected) { // viewModel.CustomerList - This object is NULL
// IsSelected - This gives the rows where checkboxes are Checked, but I look 4 only the modified
}View:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<ViewModels.CustomerViewModel>" %>
<form id="CustomerGridForm" action="/Customer/Save" method="post">
<%
int [] IsSelected = new int[] { };
Html.Telerik().Grid(Model.CustomerList)
.Name("CustomerGrid")
.Columns(columns => { columns.Bound(o => o.ID); columns.Bound(o => o.Name); columns.Template(o => { %>
<input name="IsSelected" type="checkbox" title="IsSelected" value=" <%= o.ID %>"
<%if (o.IsSelected) {%> checked="checked" <% } %> <%if (o.IsBlacklisted) {%> disabled="disabled" <% }%>
/> }); }).Render();
<button type="submit" class="t-button" value="Save" name="Save">Save</button>
</form>