8

This project uses MVC3 and Razor. We have a busy page, and the data is coming from a mainframe database via WCF services, so we want to limit what gets loaded at one time. Basic data is loaded on page load, but then there will be a few divs for additional data. We use @Html.Partial(pagename) to render a partial page in the div. This part of the ViewModel will initially be empty, so no data will display.

We want a user click to go to a Controller method, call another service, and fill in a section of the ViewModel with more data. Then refresh this section of the page with the data.

How do I refresh HTML.Partial(pagename) on a user click? I've tried an Ajax call to the appropriate URL, but it doesn't refresh the display.

Edit: here is my code, trying to use the suggestions from Darin Dimitrov below.

My ViewModel:

namespace Project.ViewModel
{
    public class AllData
    {
        public string Id { get; set; }
        public BasicData Basics { get; set; }
        public ContactInfo ContactPoints { get; set; } //mainframe data
    }
}

The Main page:

@model Project.ViewModel.AllData

 <script type="text/javascript">
    $(function () {
        $('#loadFromMainFrame').click(function (e) {
            var url = $(this).data(url);
            $('#MainframeContents').load(url);
        });
    });
</script>

<div id="basics">
    <div>
       @Html.DisplayFor(model => model.Basics.FirstName)
    </div>

    <div>
         @Html.DisplayFor(model => model.Basics.LastName)
    </div>     
</div>

<button id="loadFromMainFrame" data-url="@Url.Action("GetFromMainFrame")">Load from mainframe</button>
<div id="MainframeContents">
    <p>Contact Info will go here</p>
</div>

The Controller:

    public ActionResult Details(string id)
    {
        BasicData basicdata = new BasicData();
        basicdata.FirstName = "Arya";
        basicdata.LastName = "Stark";
        AllData allData = new AllData();
        allData.Basics = basicdata;
        return View(allData);
    }

    public ActionResult GetFromMainframe()
    {
        AllData allData = new AllData();
        allData.ContactPoints = …getting data from mainframe
        return PartialView("ContactsSection", allData);
    }

The Details page renders. Clicking the button executes the script, but the controller is not hit. What am I doing wrong?

I’ll also post some other attempts I’ve made…

tereško
  • 58,060
  • 25
  • 98
  • 150
DZx
  • 347
  • 1
  • 4
  • 11
  • 2
    It seems that you should correct the syntax on the jquery script: var url = $(this).data('url'); Note the quotes. What value does the variable url hold after this? – Francisco Jul 25 '13 at 16:03

1 Answers1

20

I've tried an Ajax call to the appropriate URL, but it doesn't refresh the display.

That's exactly what you should have to use. Let's suppose that you have the following view:

<div id="mainframeContents"></div>

<button id="loadFromMainFrame" data-url="@Url.Action("GetFromMainFrame")">Load from mainframe</button>

and then you could write the following script which will send an AJAX request to the corresponding controller action when the button is clicked and load the content in the div:

$(function() {
    $('#loadFromMainFrame').click(function(e) {
        e.preventdefault();
        var url = $(this).data("url");
        $('#mainframeContents').load(url);
    });
});

and now all that's left of course is to have the corresponding controller action that will load the data from the mainframe and render a partial view:

public ActionResult GetFromMainFrame()
{
    SomeModel model = ... go hit the mainframe to retrieve the model using a webservice or whatever you are using to hit it
    return PartialView("_SomePartial", model);
}
Boatmarker
  • 197
  • 1
  • 10
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    I'm not able to get this working. Is the button supposed to be in the parent view, or the second page which has only the partial view? I've tried both, but in neither case does my breakpoint in the controller get hit. I'll post my code shortly. – DZx Jul 25 '13 at 14:32
  • 1
    Got it working by adding the quotes, as Francisco pointed out above. Many, many thanks! – DZx Jul 25 '13 at 20:39
  • 1
    You added the quotes where? In this answer where does the url comes from? – S.H. Mar 16 '14 at 05:48
  • I think `$(this).data(url);` should be `$(this).data("url");` –  May 24 '19 at 15:19
  • Is this compatible with Url.Action("GetAsync",..) and IActionResult action methods? I'm trying to motivate using Url.Action (or HTML Helpers at all, now that Ajax Helpers have been removed), but if you have to write Javascript anyway... – Henrik Erlandsson Jun 20 '22 at 12:16