Try our conversational search powered by Generative AI!

Go to action in controller from click on a checkbox

Vote:
 

I've rendered a categorylist as a checboxlist. Now I would like to go to an action in a controller, when clicking a checkbox element. How can I do this

public class BlogPageController : PageController<>BlogPage>
    {
        public ActionResult Index(BlogPage currentPage)
        {
            
            var category = Category.Find("Post");
            var postcategories = category.Categories; /* subcategories of category Post
 
            currentPage.Categories = postcategories;

            var model = PageViewModel.Create(currentPage);
 
            return View(model);
        }
 
        [HttpPost]
        public ActionResult Index()
        {
            /*Here is the point I would like to go after clicking a checkbox*/
 
            return null;
        }
    }

Rendering categories in displaytemplate/CategoryCollection.cshtml

@using (Html.BeginForm("Index""BlogPage"FormMethod.Post))
{
 
    <>div class="row">
 
        @if ((string)@ViewData["class"== "SelectCategories")
        {
 
            foreach (var category in Model)
            {
                @*@category.Description
                    *@
 
                @*@category.Description*@
                <>label class="@ViewData["itemclass"]">@Html.CheckBox(@category.ID.ToString(), false@category.Descriptionlabel>
            }
        }
    div>
    <>button type="submit">Submitbutton>
}

The rendering is Ok, but I can't go the action breakpoint when clicking any checkbox (return null above)

 

#138927
Sep 23, 2015 19:15
Vote:
 

I've tried with a button too, without success.

My project is developed in episerver 8 mvc.

#138928
Sep 23, 2015 19:17
Vote:
 

I belive the problem lies in your routing. I'm assuming your post now goes to /BlogPage/Index, but routes for this is not included in the default episerver setup. You could add a standard controller/action/id route in your routetable to make that work, but its not the best solution since it can cause interference with the regular episerver routing.

Since episerver can handle posts to the controllers with actions specified it would probably be best to just write a standard form element with the method=post attribute and skip the action attribute (or use something like #submit). Then you should be posting to the same url that you are currently on and the HttpPost attribute on the action should make it work. If not you can try out giving the action a different name like submit and use the UrlResolver to get the url for the current page and then manually add a /sbmit at the end of that.

I havent tested it myself, but atleast it seems plausible so I would give it a shot.

#138955
Sep 23, 2015 22:28
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.