簡體   English   中英

如何從MVC4中動作返回的列表中重復填充數據?

[英]How to populate data repeatedly from list returned by action in mvc4?

我是一種新手,在讀取要從數據庫獲取並想要顯示在跨度中的數據時遇到問題。 我的問題是我已將數據帶入控制器,但現在我想在視圖中顯示該數據。 我可以訪問列表中的特定數據,但是我想像新聞一樣以重復模式顯示數據。 我的控制器部分在這里

 public ActionResult Index()
        {
            List<Quote> quote = dbContext.quotes.ToList();                
            return View(quote);                
        }

查看部分是

<blockquote> 
       @foreach 
        (var items in @Model) 
        { 
             @items.quote 
        } 
</blockquote>

我應該在此處實現哪種邏輯以重復模式獲取數據,以便每小時都能報價一次。

您可以使用location.reload(); 在javascript中,它將每小時重新加載您的頁面並獲取最新消息:

@model IEnumerable<MVCTutorial.Models.Quote>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Quotes</title>
    <script type="text/javascript">
        setInterval(function ()
        {
            location.reload();

        }, 3600000);//3600000 milliseconds = 1 hour
    </script>
</head>
<body>
    <blockquote>
        @foreach(var item in Model)
        {
            <span>@String.Format("{0}-{1}",item.ID,item.QuoteName)</span><br />
        }
    </blockquote>
</body>
</html>

編輯

要定期僅獲取最新的項目,可以使用$.getJSON進行AJAX調用,並將最新的項目附加到列表中。

控制器動作方法:

public JsonResult GetLatestNews()
{
    //Write the query here to get the latest items...
    List<Quote> quotes = dbContext.quotes.ToList();
    return Json(quotes, JsonRequestBehavior.AllowGet);
}

視圖:

@model IEnumerable<MVCTutorial.Models.Quote>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Quotes</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            setInterval(function () {
                $.getJSON("@Url.Action("GetLatestNews","Quote")", function (data) {

                    for(var i = 0;i < data.length;i++){
                        var id = data[i].ID;
                        var quoteName = data[i].QuoteName;

                        var quote = "<span>" + id + "-" + quoteName + "</span><br />";
                        $("#quotes").append(quote);
                    }
                });

            }, 3600000);//3600000 milliseconds = 1 hour
        });
    </script>

</head>
<body>
    <blockquote id="quotes">
        @foreach (var item in @Model)
        {
            <span>@String.Format("{0}-{1}", item.ID, item.QuoteName)</span><br />
        }
    </blockquote>
</body>
</html>

感謝aniruddhanath的jquery插件幫助我完成了此場景

Index.cshtml

<div id="crotator">
        <div id="quotes">
            @foreach (var news in Model)
            {    
                <p>@news.News</p>
            }
        </div>    
        <div id="authors">
            @foreach (var news in Model)
            {

                <p>- @news.Author</p>
            }    
        </div>    
    </div>

腳本塊

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://www.jqueryscript.net/demo/Simple-jQuery-Content-Rotator-Plugin-Crotator/js/jquery-crotator.js"></script>
<script>
    $("#quotes").crotator({
        // optional changes
        timeofExistence: 5000,
        typeofTag: "<h3/>",
        tagClass: "quotes"
    });
    $("#authors").crotator({
        // optional changes
        timeofExistence: 5000,
        typeofTag: "<h4/>",
        tagClass: "authors"
    });
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM