[RESOLVED]Create category list like GumTree

Hello,

I have a page where the user will choose a category, I’m building a UL list of parent categories to choose from, when the user clicks on one of those categories I need to make a call back to the controller to retrieve the sub categories.

So far my code looks like this

 @using (Html.BeginForm("SellItem", "Sell", FormMethod.Post, new { @class = "form-horizontal" }))
                                {
                                    <div class="step-content pos-rel" id="step-container">
                                        <div class="col-md-4" style="margin-top: 2%">
                                            <ul class="nav nav-pills nav-stacked " id="ulParent" style="max-height: 300px; overflow: auto;">
                                                @foreach (var item in Model.ParentCategory)
                                                {

                                                    <li data-grid-id="@item.ParentCategoryId" data-grid-value="@item.CategoryValue"><a href="/sell-item">@item.CategoryDescription <span class="glyphicon glyphicon-chevron-right"></span></a></li>

                                                }
                                            </ul>
                                        </div>
</div>
}

But when I click it, it makes a call back to the the GET method instead of the POST this is my POST inside the controller

[HttpPost]
        public ActionResult SellItem(ChooseCategoryViewModel viewModelCategory)
        {
            try
            {
                // Do something when user clicks on the categories....
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return View(viewModelCategory);
        }

Harrison.Scott

<li data-grid-id="@item.ParentCategoryId" data-grid-value="@item.CategoryValue"><a href="/sell-item">@item.CategoryDescription <span class="glyphicon glyphicon-chevron-right"></span></a></li>

usually <a> request will be with get method.

use actionlink with post method

check this link

http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/

Hi I have changed the code to look like this

                                                    <li data-grid-id="@item.ParentCategoryId" data-grid-value="@item.CategoryValue">
                                                        <a href='@Html.ActionLink("test", "SellItem", "Sell", new { area = "Profile", parentCatId = "1" })'>@item.CategoryDescription <span class="glyphicon glyphicon-chevron-right"></span></a>
                                                    </li>

I get this error

A potentially dangerous Request.Path value was detected from the client (<).

I have it working, but when I click it it keeps going back to the GET Method not the POST

 @foreach (var item in Model.ParentCategory)
                                                {

                                                    <li data-grid-id="@item.ParentCategoryId" data-grid-value="@item.CategoryValue">
                                                        
                                                        @Html.ActionLink(item.CategoryDescription, "SellItem", "Sell", new { parentCatId = item.ParentCategoryId, area = "Sell" }, new { @class = "btn btn-success" })
                                                    </li>

                                                }

Controller

 [HttpPost]
        public ActionResult SellItem(string parentCatId)
        {
            var t = parentCatId;

            try
            {
              //Do something
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            //viewModelCategory

            return View();
        }

Harrison.Scott

A potentially dangerous Request.Path value was detected from the client (<).

use like this

  <li data-grid-id="@item.ParentCategoryId" data-grid-value="@item.CategoryValue">
            @Ajax.ActionLink(@item.CategoryDescription, "SellItem", new { area = "Profile", parentCatId = "1" }, new AjaxOptions { HttpMethod = "post" }) <span class="glyphicon glyphicon-chevron-right"></span>
        </li>

SellItem action does not have to be post action. it is just to get subcats

Leave a Reply