我有一个将列出名称的 MVC 应用程序。这些名称位于实体框架数据库中。计时器在列表中的第一个名称旁边,当计时器结束时,该名称从列表中删除,并且计时器再次开始下一条记录(此过程一直持续到没有名称为止)。
我还可以将名称添加到列表中。现在,当用户通过单击创建链接将名称添加到列表中时,会添加名称,但它会重新启动当前正在倒计时的计时器。我需要在不刷新计时器的情况下添加名称。那可能吗??
看法:
@model IEnumerable<RangeTimer.Models.UserName>
@{
ViewBag.Title = "ABC";
}
<div class="jumbotron">
<p>
@Html.ActionLink("Add Name to list for range time", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FullName)
</th>
<th>
Time Remaining
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td id="FullName">
@Html.DisplayFor(modelItem => item.FullName)
</td>
<td>
<span id="timer"></span>
</td>
</tr>
}
</table>
</div>
<br/>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
startTimer();
function startTimer() {
$('#timer').countdown({
layout: '{mnn} : {snn}', timeSeparator: ':', until: 15, onTick: TimerColorChange, onExpiry: restartTimer
});
}
function restartTimer() {
$('#timer').countdown('destroy');
var currentName = $('#FullName').Text;
//we delete the table's Info
var deleteRecord = $('#FullName').parent().remove();
// deleteRecord.deleteRow(); //commented out since this also removes my timer
var action = '@Url.Action("DeleteName","Controller")';
$.get(action + "?name=" + currentName).done
(function (result) {
if (result) {
// your user is deleted
}
})
startTimer();
}
function TimerColorChange(periods) {
var seconds = $.countdown.periodsToSeconds(periods);
if (seconds <= 3) {
$(this).css("color", "red");
} else {
$(this).css("color", "black");
}
}
});
</script>
控制器
// GET: UserNames
public ActionResult Index()
{
return View(db.UserNames.ToList());
}
// GET: UserNames/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UserName userName = db.UserNames.Find(id);
if (userName == null)
{
return HttpNotFound();
}
return View(userName);
}
// GET: UserNames/Create
public ActionResult Create()
{
return View();
}
// POST: UserNames/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,FullName")] UserName userName)
{
if (ModelState.IsValid)
{
db.UserNames.Add(userName);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(userName);
}