0

i'm using .Net core 2.1 application, inside for loop how can i trigger controller view page without returning to ajax response, see following code

this is my razor view

  @foreach (var item in Model.CategoryList)
                    {
                        <div class="col-sm-6 col-md-4 mb-4 mb-lg-0 col-lg-2"  onclick="SelectCategory(@item.CategoryId)">
                            <a href="#" class="popular-category h-100">
                                <span class="icon"><span class="@item.Icon"></span></span>
                                <span class="caption mb-2 d-block">@item.CategoryName</span>
                                <span class="number">32,891</span>
                            </a>
                        </div>
                    }





<script>

        function SelectCategory(id) {

            $.ajax({
                type: 'POST',
                url: '@Url.Action("ProductsList","Home")',
                dataType: 'json',
                data: { parentId: id }
            });
            
        }
    </script>




 public ActionResult ProductsList(int parentId)
        {
            List<PilCategory> all = new List<PilCategory>();
           
            if(parentId > 0)
                all = _context.PilCategory.Where(x=>x.ParentId == parentId).OrderBy(a => a.ParentId).ToList();
            else
                all = _context.PilCategory.OrderBy(a => a.ParentId).ToList();

            return View(all);
        }

pls someone help me, thaks for advance

1 Answer 1

2

how can i trigger controller view page without returning to ajax response

As far as I know, we couldn't trigger controller view page without returning to ajax response. Since the ajax will send the request to the controller view and the controller view will return the view as html response back to the ajax.

If we don't handle the response, that means current page will not change.

If you just want to pass the parentId to the ProductsList method, I suggest you could try to use window.location, this will make this page redirect to the ProductsList view which achieve refresh the whole page.

Notice: If we using ajax to call ProductsList, the whole page will not refresh. If we using window.location, it will redirect to another page or refresh the whole page.

More details about how to achieve this, you could refer to below codes:

@section Scripts{
    <script>
        function SelectCategory(id) {
            location.href = "/Home/ProductsList?parentId=" + id;
        }
    </script>
}

If you want to use ajax to achieve this, I suggest you could try to handle the response by using ajax success method. Normally, we will return the partial view in controller method and then we could add it into the html page by using jquery without refreshing the page.

More details about how to achieve this, you could refer to this answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.