2016-03-03 135 views
0

我有一個將列出名稱的MVC應用程序。 (名稱位於實體框架數據庫中。)計時器位於列表中的名字旁邊,當計時器結束時,名稱將從列表中刪除,列表中的第二個名稱將向上移動,計時器重新開始(這繼續直到沒有名字被留下)。我需要添加從EF數據庫中刪除名稱的功能。有任何想法嗎?使用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"); 
     } 
    } 


}); 

型號代碼:

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

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


    </td> 

</tr> 
} 

</table> 

控制器:

private UserNameDBContext db = new UserNameDBContext(); 



    // GET: UserNames 
    public ActionResult Index() 
    { 


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

很多想法,但我不打算爲你做所有的研究和調整工作,所以這裏有一些想法。 (1)jQuery不能直接修改數據庫。 (2)讓jQuery調用控制器動作並讓控制器動作修改數據庫。 –

+0

謝謝你的想法。請放心,我並沒有要求你做所有的研究和工作。我研究了這是爲什麼我張貼在這裏謝謝。 – user2448412

回答

0

我會給你一個小爲例:

1)添加一個方法來刪除用戶名在你在你的JavaScript控制器

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

2)調用該函數與一個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(); 
} 
相關問題