2

Im trying to open modal in specific path using asp.net mvc 4 + jquery, heres the code:

Card.cshtml

<div class="btn-group">
    <button class="btn btn-primary openModal" data-path="/Client/Edit/@Model.ClientID">Editar</button>
    <button class="btn btn-primary openModal" data-path="/Client/Products/@Model.ClientID">Products</button>
</div>

then in modal.js

$(document).on('click', '.openModal', function () {
    var path = $(this).attr("data-path");
    $("#modal").load(path, function () {
        $("#modal").modal();
    })
});

in ClientController

public ActionResult Edit(long id = 0)
{
    Client client = db.Clients.Find(id);

    if (client == null)
    {
        return HttpNotFound();
    }

    return View(client);
}

public ActionResult Products(long clientID)
{
    return View(db.Products.Where(p => p.ClientID == clientID).ToList());
}

The Edit action works, but the Products action not (i put a breakpoint and its not called). Whats wrong?

2
  • Did you check the response, is there any error? Commented May 24, 2016 at 18:27
  • The browser just "fade" like when "Edit" Modal open, but not happen, the controller's "Products Action" is not called, but cheking on chrome the "path" property of jquery code is right Commented May 24, 2016 at 18:30

1 Answer 1

0

The problem was the name of parameter

public ActionResult Products(long clientID)
{
    return View(db.Products.Where(p => p.ClientID == clientID).ToList());
}

Changed from clientID to just id and works. It's because default routes is

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
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.