MVC,如何从响应viewModel获取模型属性

时间:2017-08-23 07:11:42

标签: javascript c# jquery asp.net-mvc

我正在我的博客列表中实施“显示更多帖子” this example但我想进行更改并从使用ViewData切换到ViewModel

这是相关代码:

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

我切换到

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

示例的索引操作:

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);
}

这里我添加了很多代码来从db加载帖子,我认为唯一相关的代码就是在设置return View("EntryTeaserList", pagedEntries);和{{return View(myViewModel);之后我将BlogsList转换为ShowMore 1}}属性。

但我的问题出在javascript命令中:

$(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;
});
}

,其中

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

获取ViewData属性并使用它来替换链接。

如何更改地阅读ViewModel属性?

0 个答案:

没有答案