Question

I am building a page where a user can view the details of an item which s/he selected.

As part of this project, I need to show all the comments that are listed under this particular item using Ajax and partial views.

In the Controller class, I am somehow unable to pass the productID of the selected item to the partial view method. When I hard code the productID into the method, the comments show up, however when I pass it through the parameter, the method won't even trigger.

All the product details, however, show without restrictions.

I would appreciate any help. Below please find the code in my Controller

    public ActionResult Index()
    {
        List<Product> productList = new ProductClient().GetAllProducts().ToList();
        return View("Index", productList);
    }


    //This method works correctly.  The id of the product is passed.
    public ActionResult Details(int id)
    {
        return View(new ProductClient().GetProductByID(id));
    }


    // This method is not even getting triggered.
    public PartialViewResult ProductComments(int id)
    {
        List<Comment> commentList = new ProductCommentClient().GetCommentsByProductID(id).ToList();
        return PartialView("_comments", commentList);
    }

This is my Details.cshtml

@Ajax.ActionLink("Product Comments", "ProductComments(" + @Model.ID + ")", new   AjaxOptions
{
HttpMethod = "Get",
UpdateTargetId= "divComments",
InsertionMode = InsertionMode.InsertAfter
})

<fieldset>
<div id="divComments">
    <legend>Comments</legend>
</div>
</fieldset>

Many thanks in advance.

Was it helpful?

Solution

I solved this.

@Ajax.ActionLink("Product Comments", "ProductComments", new {id=Model.ID}, new   AjaxOptions
{
    HttpMethod = "Get",
    UpdateTargetId= "divComments",
    InsertionMode = InsertionMode.InsertAfter
})

<fieldset>
    <div id="divComments">
        <legend>Comments</legend>
    </div>
</fieldset>

I was passing the ID in the wrong manner. I hope that this would at least help somebody else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top