我有一个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());
}
答案 0 :(得分:0)
我会给你一个小例子:
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();
}