0

I am implementing "show more posts" in my blogs list following this example but I want to make a change and switch from using ViewData to ViewModel.

This the relevant code:

private void AddMoreUrlToViewData(int entryCount)
{
    ViewData["ShowMore "] = Url.Action("Index", "Messaggi", new { entryCount = entryCount + defaultEntryCount });
}

that I switch to

ViewModel.ShowMore = Url.Action("Index", "Messaggi", new { entryCount = entryCount + defaultEntryCount });

the Index action of the example:

public ActionResult Index(int? entryCount)
{
if (!entryCount.HasValue)
    entryCount = defaultEntryCount;

int totalItems;

if(Request.IsAjaxRequest())
{
    int page = entryCount.Value / defaultEntryCount;

    //Retrieve the page specified by the page variable with a page size o defaultEntryCount
    IEnumerable<Entry> pagedEntries = GetLatestEntries(page, defaultEntryCount, out totalItems);

    if(entryCount < totalItems)
        AddMoreUrlToViewData(entryCount.Value);

    return View("EntryTeaserList", pagedEntries);
}

//Retrieve the first page with a page size of entryCount
IEnumerable<Entry> entries = GetLatestEntries(1, entryCount.Value, out totalItems);

if (entryCount < totalItems)
    AddMoreUrlToViewData(entryCount.Value);

return View(entries);
}

Here I add a lot of code to load the post from the db, the only code I think is relevant is that I swtich return View("EntryTeaserList", pagedEntries); to return View(myViewModel); after setting BlogsList and ShowMore property.

But my problem is in the javascript command:

$(function() {
    addMoreLinkBehaviour();
});

function addMoreLinkBehaviour() {
$('#entryTeaserList #moreLink').live("click", function() {
    $(this).html("<img src='/images/ajax-loader.gif' />");
    $.get($(this).attr("href"), function(response) {
        $('#entryTeaserList ol').append($("ol", response).html());
        $('#entryTeaserList #ShowMore').replaceWith($("#ShowMore", response));
    });
    return false;
});
}

where

$('#entryTeaserList #ShowMore').replaceWith($("#ShowMore", response));

to take ViewData property and use it to replace the link.

How can I read the ViewModel Property instead?

RickL
  • 3,318
  • 10
  • 38
  • 39
gt.guybrush
  • 1,320
  • 3
  • 19
  • 48
  • This might be helpful https://stackoverflow.com/a/41312348/2592042 look into the section `object assignment point 6` – Rajshekar Reddy Aug 23 '17 at 07:14
  • i know how to read from model of the view, i don't know how to access the model object of response that is not rendered – gt.guybrush Aug 23 '17 at 07:19
  • That post exactly describes the code you need to do that.. – Rajshekar Reddy Aug 23 '17 at 07:48
  • this more: https://stackoverflow.com/questions/38522774/get-model-count-in-ajax-response-returning-partial-view – gt.guybrush Aug 23 '17 at 07:54
  • Okay I get it. I understood the question wrong.. you need to pass the result as a JSON and not a view. So when you pass as JSON you can use your `response` variable directly to access the values just like how you would use a javascript object. But if you have no option other than using a view then you will have a write a javascript code (in your ajax response view) to assign the model value into a variable and then append your response to some html (when you do this all the scripts will be executed). With this you can now access your javascript variable. let me know if you find it difficult – Rajshekar Reddy Aug 23 '17 at 08:23
  • just got it reading well the discussion linked: response is simply html so i just need to get the id i need and use this element to replace my previous – gt.guybrush Aug 23 '17 at 08:58

0 Answers0