[英]Delete record from Entity Framework database with jQuery
我有一个MVC应用程序,它将列出名称。 (名称在实体框架数据库中。)计时器在列表中的第一个名称旁边,当计时器结束时,该名称将从列表中删除,列表中的第二个名称将向上移动,计时器重新开始(直到没有名字留下为止。 我还需要添加从EF数据库中删除名称的功能。 有任何想法吗?
jQuery查询
$(document).ready(function () {
startTimer();
function startTimer() {
$('#timer').countdown({
layout: '{mnn} : {snn}', timeSeparator: ':', until: 5, onTick: TimerColorChange, onExpiry: restartTimer
});
}
function restartTimer() {
$('#timer').countdown('destroy');
//we delete the table's Info
var deleteRecord = $('#firstName').parent().remove();
// deleteRecord.deleteRow(); //commented out since this also removes my timer
startTimer();
}
function TimerColorChange(periods) {
var seconds = $.countdown.periodsToSeconds(periods);
if (seconds <= 3) {
$(this).css("color", "red");
} else {
$(this).css("color", "black");
}
}
});
型号代码:
public class UserName
{
public int Id { get; set; }
[Display(Name = "Full Name")]
public string FullName { get; set; }
}
public class UserNameDBContext : DbContext
{
public DbSet<UserName> UserNames { get; set; }
}
视图:
@model IEnumerable<RangeTimer.Models.UserName>
@{
ViewBag.Title = "";
}
<br />
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FullName)
</th>
<th>
Time Remaining
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td id="firstName">
@Html.DisplayFor(modelItem => item.FullName)
</td>
<td id="timer">
<span id="timer"></span>
</td>
</tr>
}
</table>
控制器:
private UserNameDBContext db = new UserNameDBContext();
// GET: UserNames
public ActionResult Index()
{
return View(db.UserNames.ToList());
}
我给你一个例子:
1)添加一种方法来删除控制器中的用户名
// GET: Delete
public JsonResult DeleteName(string name)
{
//Delete your user from your context here
return Json(true, JsonRequestBehavior.AllowGet);
}
2)在您的javascript中,使用ajax请求调用此函数:
function restartTimer() {
$('#timer').countdown('destroy');
//we delete the table's Info
var deleteRecord = $('#firstName').parent().remove();
// deleteRecord.deleteRow(); //commented out since this also removes my timer
var action ='@Url.Action("DeleteName","Controller")';
$.get(action+"?name="+nameToDelete).done(function(result){
if(result){
// your user is deleted
}
})
startTimer();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.