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

[Solved] Checked checkboxes not posting back to controller action

2 Answers 61 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Devang
Top achievements
Rank 1
Devang asked on 01 Sep 2011, 03:39 PM

The issue i am having is that i have a MVC treeview control on an ASPX page with ShowCheckBox set to true. I check off a couple of the check boxes and post the form to my controller action, the parameter of the list of TreeViewItems that is supposed to be populated with the TreeViewItems that had their checkboxes checked off is always null, please HELP!!!!

Index.aspx

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewData["Message"] %></h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc<;/a>.
    </p>
    <% using (Html.BeginForm("Test", "Home", FormMethod.Post)) %>
       <% { %>
   <%= Html.Telerik().TreeView()
        .Name("TestTree")
        .ShowCheckBox(true)
        .Items(item =>
            {
                item.Add()
                    .Text("Mail")
                    .Items(subItem =>
                    {
                        subItem.Add()
                               .Text("Personal Folders");
                    });
            })
     %>
    <input type="submit" value="submit" />  
    <% } %>
</asp:Content>

Site.Master

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <%= Html.Telerik().StyleSheetRegistrar()
                      .DefaultGroup(group => group
                          .Add("telerik.common.css")
                          .Add("telerik.vista.css"))
    %>
   <script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery-1.5.1.min.js")%>"></script>
</head>
 
<body>
    <div class="page">
 
        <div id="header">
            <div id="title">
                <h1>My MVC Application</h1>
            </div>
               
            <div id="logindisplay">
                <% Html.RenderPartial("LogOnUserControl"); %>
            </div>
             
            <div id="menucontainer">
             
                <ul id="menu">             
                    <li><%: Html.ActionLink("Home", "Index", "Home")%></li>
                    <li><%: Html.ActionLink("About", "About", "Home")%></li>
                </ul>
             
            </div>
        </div>
 
        <div id="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />
 
            <div id="footer">
            </div>
        </div>
    </div>
     
     <%= Html.Telerik().ScriptRegistrar().DefaultGroup(group =>
            {
                group.Add("telerik.common.js");         // That file is always required
                group.Add("telerik.treeview.js");
                
            })
        %>
</body>
</html>

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;
 
namespace TestTelerikTreeView.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
 
            return View();
        }
 
        public ActionResult About()
        {
            return View();
        }
 
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Test(List<TreeViewItem> ti)
        {
            return null;
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Alex Gyoshev
Telerik team
answered on 02 Sep 2011, 07:23 AM
Hi Devang,

The name of the parameter in your controller action is wrong. It must be in the form <TreeviewName>_checkedNodes, in your case - TestTree_checkedNodes.

Regards,
Alex Gyoshev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Devang
Top achievements
Rank 1
answered on 06 Sep 2011, 03:54 PM
Hi Alex,

Thanks for your prompt reply, your fix helped me save countless hours of debugging and at the same time taught me something i didn't know about MVC Model Binding. Thanks again!
Tags
TreeView
Asked by
Devang
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Devang
Top achievements
Rank 1
Share this question
or