Retrieve the last 5 ids of the Sql Server database using entity framework?

-2

How do I retrieve information from the last 5 Ids of the Sql server database using entity framwork?

Controller

public class VideoController : Controller
{
    private DBContext db = new DBContext();

    // GET: Video
    public ActionResult Index()
    {
        var lista = db.Videos.ToList();
        return View(lista);
    }


    public ActionResult SalvarItens(string titulo, string video_url) 
    { 

        var item = new Video()
        {
            Titulo = titulo,
            UrlVideo = video_url
        };

        db.Videos.Add(item);
        db.SaveChanges();

        return Json(new { Resultado = item.Id }, JsonRequestBehavior.AllowGet);
    }
}

DBContext

public class DBContext : DbContext 
{

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }

    public System.Data.Entity.DbSet<Teste.Models.Produto> Produtoes { get; set; }

    public System.Data.Entity.DbSet<Teste.Models.Video> Videos { get; set; }
}

Index.cshtml

<section id="main">
    <input type="text" id="titulo" name="titulo" value="" required="" placeholder="Titulo" />

        <input type="text" id="" name="url" required="" value="" placeholder="url" />

        <button id="submit" onclick="SalvarItens();">Enviar Video</button>

            <div id="result">
            </div>

            <table class="table">

                   @foreach (var item in Model)
                   {
                            <tr>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Titulo)
                                </td>
                                <td>
                                  @Html.DisplayFor(modelItem => item.UrlVideo)
                                  <img src="#" alt="Alternate Text" />

                                </td>
                                <td>
                                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                                </td>
                            </tr>
                    }
            </table>

    <script>
        function SalvarItens() {
            var titulo = $("#titulo").val();
            var video_url = $("#video-url").val();
            var url = "/Video/SalvarItens";

            $.ajax({
                url: url
                , data: { titulo: titulo, video_url: video_url }
                , type: "POST"
                , datatype: "Json"
                , success: function (data) {
                    if (data.Resultado > 0) {
                        alert("Salvo");
                    }
                }
            });
        }
    </script>
    
asked by anonymous 03.09.2018 / 14:18

1 answer

0

If you are in need of the latest videos, just search this way:

var videos = db.Videos.OrderByDesc(a => a.Id).Take(5).ToList();

If you are in need of the latest products, just look like this:

var produtos = db.Produtoes.OrderByDesc(a => a.Id).Take(5).ToList();

context refers to your entity framework context.

Videos or Produtoes refers to the DbSet that is in its context which is the table you want to retrieve the data.

    
03.09.2018 / 14:34