我有一个MVC应用程序,它将列出名称。名称位于实体框架数据库中。计时器位于列表中的第一个名称旁边,当计时器结束时,名称将从列表和数据库中删除(这将持续到没有名称留下)。应用程序首先显示数据库中的5个名称。删除第一个名称时遇到困难,如何将数据库中的下一个名称附加到表中。
例如:如果显示记录1,2,3,4,5并删除记录1,我需要显示记录2,3,4,5,6。这是我现在的代码。
Index.cshtml
@model IEnumerable<RangeTimer.Models.UserName>
@{
ViewBag.Title = "";
}
<div class="container"></div>
<div class="jumbotron">
<h2>Add Title</h2>
<p>
@Html.ActionLink("Add Name to list for range time", "AddUserName", new{ target = "_blank" })
</p>
<hr />
<table class="table" id="NameList">
<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>
@Html.HiddenFor((modelItem => item.Id))
<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() {
var Id = $("#item_Id").val();
$('#timer').countdown('destroy');
//we delete the table's Info
$('#FullName').parent().remove();
// deleting records from entity framweork database;
$.ajax({
url: '@Url.Action("Delete", "UserNames")',
type: "POST",
data: ({ Id: Id }),
dataType: "json",
cache: false,
async: true,
success: function (data) {
//repopulate list
$.each(data, function (index, item) {
row += "<tr><td>" + item.FullName + "</td></tr>";
});
alert(FullName).val();
$('#NameList').html(row);
$('#NameList > tbody:last-child').append("<tr><td>"+ $('#FullName').val()+"</td> </tr>");
// $('#NameList').html(data);
}
});
startTimer();
}
function TimerColorChange(periods) {
var seconds = $.countdown.periodsToSeconds(periods);
if (seconds <= 3) {
$(this).css("color", "red");
} else {
$(this).css("color", "black");
}
}
});
</script>
控制器:
public class UserNamesController : Controller
{
private UserNameDBContext db = new UserNameDBContext();
// GET: UserNames
public ActionResult Index()
{
return View(db.UserNames.Take(5).ToList());
}
// GET: AddEmployee
public ActionResult AddUserName()
{
return View();
}
//Post method to add details
[HttpPost]
public ActionResult AddUserName(UserName obj)
{
AddDetails(obj);
TempData["Message"] = obj.FullName + " has been added to the list successfully.";
ModelState.Clear();
return View();
}
private void AddDetails(UserName obj)
{
db.Database.ExecuteSqlCommand("GetAddName @FullName", new SqlParameter("@FullName", obj.FullName));
}
[HttpPost]
public ActionResult Delete(int Id)
{
//try
//{
// TODO: Add delete logic here
UserName userName = db.UserNames.Find(Id);
db.UserNames.Remove(userName);
db.SaveChanges();
//return View(db.UserNames.Take(5).ToList());
return Json(new { success = true });
}
catch
{
return Json(new { success = false });
}
}
答案 0 :(得分:0)
以下更改应该为您提供所需的行为,但是,更新非常快,因此删除行和添加的新行之间的更改可能会被忽略(但这对于使用加载动画进行排序很容易)。
控制器:
SCNMaterial *silverMaterial = [SCNMaterial material];
silverMaterial.diffuse.contents = [UIImage imageNamed:@"silver-background"];
SCNBox *horizontalBarOne = [SCNBox boxWithWidth:11 height:0.5 length:0.5 chamferRadius:0];
SCNNode *horizontalBarOneNode = [SCNNode nodeWithGeometry:horizontalBarOne];
horizontalBarOneNode.position = SCNVector3Make(0, 2.0, 0.5);
horizontalBarOne.materials = @[silverMaterial];
[scene.rootNode addChildNode:horizontalBarOneNode];
Index.cshtml:
[HttpGet]
public ActionResult GetNames()
{
return Json(db.UserNames.Take(5).ToList(), JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult Delete(int id)
{
// delete user:
UserName userName = db.UserNames.Find(id);
db.UserNames.Remove(userName);
db.SaveChanges();
// TODO: find new user here and return as JSON:
return Json(new UserName { FullName = "User 6", Id = 6 });
}