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>