2013-04-10 1 views
1

ASP.NET MVC4 및 Entity Framework를 사용하여 인트라넷 웹 응용 프로그램을 개발하고 있습니다. 나는 내 데이터베이스에있는 모든 "인물"을 열거하고 편집하거나 삭제할 수있는 뷰를 가지고 있습니다. 모달 폼을 사용하여 액션 삭제를 확인하려고합니다. (그래서 액션 "삭제"를 사용합니다.) 그러나 그렇게하기 위해서는 삭제할 사람의 ID를 검색해야합니다.MVC 4가 부트 스트랩 모달 형식으로 데이터 전달

예제 및 설명서를 읽었을 때 삭제하려는 사람의 ID를 retireve 할 수있는 올바른 방법을 찾지 못했습니다.

내 삭제 작업 :

public ActionResult Delete(long id) 
    { 
     Person person = db.Persons.Single(p => p.Id_Person == id); 
     if (person == null) 
     { 
      return HttpNotFound(); 
     } 

     db.Persons.DeleteObject(person); 
     db.SaveChanges(); 

     return RedirectToAction("Index"); 
    } 

내보기 (모달) :

@model IEnumerable<BuSIMaterial.Models.Person> 

@{ 
    ViewBag.Title = "Index"; 
} 

@using PagedList.Mvc; 
@using PagedList; 

<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" /> 


<h2>Index</h2> 

<br /> 

<div class="group"> 
    <div class ="btn-group"> 
     <input type="button" value="New person" class="btn" onclick="location.href='@Url.Action("Create")';return false;"/> 
     <input type="button" value="Download report" class="btn" onclick="location.href='@Url.Action("PersonReport")';return false;"/> 
    </div> 
</div> 


@using (Html.BeginForm("SelectedPersonDetails", "Person")) 
{ 
    <form class="form-search"> 
     <label for="person">Search an employee : </label> 
     <input type="text" id="tbPerson" name="tbPerson" class="input-medium search-query"> 
     <button type="submit" class="btn">Search</button> 
    </form> 
} 


<br /> 

@Html.PagedListPager((IPagedList)ViewBag.PageOfPersons, page => Url.Action("Index", new { page })) 

<br /> 

<table class="table table-striped"> 
<thead> 
    <tr> 
     <th> 
      First Name 
     </th> 
     <th> 
      Last Name 
     </th> 
     <th> 
      National Number 
     </th> 
     <th> 
      Start Date 
     </th> 
     <th> 
      End Date 
     </th> 
     <th> 
      Actions 
     </th> 
    </tr> 
</thead> 

<tbody> 
@foreach (BuSIMaterial.Models.Person item in ViewBag.PageOfPersons) 
{ 
    <tr> 
     <td> 
      @item.FirstName 
     </td> 
     <td> 
      @item.LastName 
     </td> 
     <td> 
      @item.NumNat 
     </td> 
     <td> 
      @item.StartDate.ToShortDateString() 
     </td> 
     <td> 
      @if (item.EndDate.HasValue) 
      { 
       @item.EndDate.Value.ToShortDateString() 
      } 
     </td> 
     <td> 
      <div class="btn-group"> 
        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> 
         Actions 
         <span class="caret"></span> 
        </a> 
        <ul class="dropdown-menu"> 
         @{ 

          <li>@Html.ActionLink("Edit", "Edit", new { id = item.Id_Person })</li> 
          <li>@Html.ActionLink("Details", "Details", new { id = item.Id_Person })</li> 
          <li>@Html.ActionLink("Desactivate", "Desactivate", new { id = item.Id_Person })</li> 
          <li><a href="#myModal" data-toggle="modal" data-id="@item.Id_Person">Delete</a></li> 
         } 
        </ul> 
      </div> 
     </td> 
    </tr> 
} 
</tbody> 
<tfoot> 
    <tr> 
     <th> 
      First name 
     </th> 
     <th> 
      Last name 
     </th> 
     <th> 
      National number 
     </th> 
     <th> 
      Start date 
     </th> 
     <th> 
      End date 
     </th> 
     <th> 
      Actions 
     </th> 
    </tr> 
</tfoot> 
</table> 

<div>@Html.PagedListPager((IPagedList)ViewBag.PageOfPersons, page => Url.Action("Index", new { page }))</div> 

    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
     <h3 id="myModalLabel">Deleting an employee</h3> 
     </div> 
     <div class="modal-body"> 
     <p>Are you sure you want to delete this person? All the concerned data also will be deleted.</p> 
     </div> 
     <div class="modal-footer"> 
     <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> 
     <button class="btn btn-primary">Delete</button> 
     </div> 
    </div> 


@section Scripts 
{ 
    @Scripts.Render("~/bundles/jqueryval") 
    @Scripts.Render("~/bundles/jqueryui") 
    @Styles.Render("~/Content/themes/base/css") 

    <script type="text/javascript" language="javascript"> 
     $(document).ready(function() { 
      $('#tbPerson').autocomplete({ 
       source: '@Url.Action("AutoComplete")' 
      }); 
     }) 

     $('#myModal').modal(options) 

    </script> 
} 

나는 data-id ="@item.Id_Person"를 수행하여 ID를 얻을 수 있지만, 나는 마지막으로 전화를 사용하는 방법에 대한 아무 생각이 없다 내 행동. 어떤 아이디어 야?

답변

1

jQuery와 같은 것을 사용하여 Ajax Delete를 수행 할 것인지 또는 ASP.NET MVC 만 사용하려는 것인지 확실하지 않습니다.

자바 스크립트를 사용하려면 data-id에서 ID를 가져오고 $.ajax을 사용하여 컨트롤러에 삭제 요청을 보냅니다. 당신이 MVC와 Ajax를 사용하려면

난 그냥 보통의 HTML.ActionLink

을 할 것입니다 단지 다음 다음 RedirectToAction을 삭제하려면 당신은 아마 Ajax.ActionLink

필요하다고 생각