0

I have a page below with buttons and a div pageContent to host my partial views

        div id="ProjID">
            <div id="pjls">
                <label for="SelectProjID">Project:</label>

            </div>
        </div>

        <button class="links" data-url='@Url.Action("link1method","Home")'>Link1</button>
        <button class="links" data-url='@Url.Action("link2method","Home")'>Link2</button>
        <button class="links" data-url='@Url.Action("link3method","Home")'>Link3</button>


        <div id="pageContent"></div>

I have this jquery that is used to load the partial views and their content

        $('.links').on('click', function (e) {
                e.preventDefault();

                var $pageContent = $('#pageContent');
                    url = $(this).data('url');

                    $.get(url, function (data) {
                        $pageContent.replaceWith(data);               
                    });

            })

Below is what my controller methods look like

            public ActionResult link1method(string sample)
            {

                return PartialView("_Partial1");
            }

            public ActionResult link2method(string sample)
            {

                return PartialView("_Partial2");
            }

            public ActionResult link3method(string sample)
            {

                return PartialView("_Partial3");
            }

At the moment when you click the Links button, it is only Link1 that loads the partial view others do not work. when you click on buttons 2 and 3 nothing happens. What am I doing wrong? I will also like to pass a parameter to each controller method.

2 Answers 2

1

If you click "Link1" first, your replacing <div id="pageContent"></div> with the partial that the method returns so that element no longer exists. If you then click "Link2", var $pageContent = $('#pageContent'); returns undefined so there is nothing to update.

Change the $.get() code to

$.get(url, function (data) {
    $pageContent.html(data); // replaces the inner contents of the element   
})

Side note: Your controller methods have a parameter string sample but you never pass a value for that to the method. You would need to use

$.get(url, { sample: "someValue" }, function (data) {
Sign up to request clarification or add additional context in comments.

3 Comments

I have a question for you please. If I decide to load the content of the first partial view by default. I know this has to be in the document.ready function. How do I go about writing this?
No, Just use <div id="pageContent">@{ Html.RenderAction("link1method","Home", new { sample = "someValue" }); }</div> to load in initially.
Thanks. I will try that now.
0

In your get function, you need to pass your parameter data. One way is if you had the data at the end of the url in the query string. Another method is by passing the parameter info in a JSON object like this:

var jsonObject = { sample : 'parameter value to send'  };

$.get(url, jsonObject, function (data) {
    $pageContent.replaceWith(data); 
});

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.