简体   繁体   English

使用jQuery从Entity Framework数据库中删除记录

[英]Delete record from Entity Framework database with jQuery

I have a MVC application that will list names. 我有一个MVC应用程序,它将列出名称。 (The names are in an entity framework database.) A timer is beside the first name in the list and when the timer ends, the name is removed from the list and the second name in the list will move up and the timer starts over (this continues until no names are left). (名称在实体框架数据库中。)计时器在列表中的第一个名称旁边,当计时器结束时,该名称将从列表中删除,列表中的第二个名称将向上移动,计时器重新开始(直到没有名字留下为止。 I need to add the ability to remove the names from the EF database also. 我还需要添加从EF数据库中删除名称的功能。 Any ideas? 有任何想法吗?

JQuery 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");
        }
    }


});

Model code: 型号代码:

 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; }
}

View: 视图:

@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">

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="timer"></span>


    </td>

</tr>
}

</table>

Controller: 控制器:

 private UserNameDBContext db = new UserNameDBContext();



    // GET: UserNames
    public ActionResult Index()
    {


        return View(db.UserNames.ToList());
    }

I ll give you a little exemple : 我给你一个例子:

1) Add a method to delete the username in your controller 1)添加一种方法来删除控制器中的用户名

// GET: Delete
public JsonResult DeleteName(string name)
{
    //Delete your user from your context here 
    return Json(true, JsonRequestBehavior.AllowGet);
}

2) in your javascript call this function with an ajax request : 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM