.NET MVC 阻止创建新记录时刷新

本文关键字:新记录 刷新 创建 MVC NET | 更新日期: 2023-09-27 18:37:22

我有一个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);
    }

.NET MVC 阻止创建新记录时刷新

如果要将时间参数发送到 addname 操作,并且必须添加重定向参数。而不是使用viewbag.time。

是的,这是可能的!通过 Ajax 和 Javascript/jQuery 的魔力,您可以在通过 ajax 添加名称时调用控制器以提交名称并返回添加的名称并使用 Javascript 或 jQuery 将其附加到页面。不幸的是,这有点复杂,我目前无法为您写出来。但是,请尝试以下一些链接。

https://msdn.microsoft.com/en-us/library/dd381533(v=vs.100).aspx

在 asp.net mvc 中对控制器进行简单的 Ajax 调用

祝你好运!

编辑:下面是一个使用 jQuery/ajax 的 javascript 函数的一个小示例。

function AddName(name) {
    $.ajax({
        url: action, // Make it whatever your controller is
        data: { UserName: name }, // or whatever object you need
        type: "POST",
        dataType: "html",
        success: function (result, status, blah) {
            if (result) {
                AppendRow(result, target); // Create function to add to your name list
            }
        },
        error: function (result) {
            AjaxFailed(result, result.status, result.error);
        }
    });
}