2016-03-23 55 views
2

我在學習如何使用ajax調用與顯示partialview的引導模態對話框。我創建了一個簡單的MVC 5應用程序。我可以從我父視圖上PERSON1編輯按鈕模態對話框OK即MVC 5使用部分視圖的JQuery Bootstrap模態

  1. 點擊內編輯記錄,調用編輯(GET)控制器動作,並顯示與PERSON1細節模式。
  2. 如果我更改年齡值並單擊保存,它會調用編輯(發佈)控制器操作並更新Person1的年齡。
  3. 我可以爲Person2做同樣的事情。

    但是,如果我試圖再次編輯相同的記錄(即Person1),它將顯示模式對話框,但它不會從ajax get調用控制器操作,並顯示Person2的數據,即我編輯的最後一條記錄。

我做錯了什麼?我已經在下面發佈了我的代碼。

父視圖(index.cshtml)

@using WebApplication1.Models; 

@model List<PersonViewModel> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

    <table> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td id="[email protected]"> 
       @Html.Partial("ListItem", item) 
      </td> 
     </tr> 
    } 
</table> 

<div class="modal" id="editor-container" tabindex="-1" role="dialog" aria-labelledby="editor-title"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content" id="editor-content-container"></div> 
    </div> 
</div> 

@section scripts 
{ 
    <script type="text/javascript"> 
     $(function() { 
      $('.editor-container').click(function() { 
       var pid = $(this).attr('data-id'); 
       var url = "/Person/Edit/" + pid; 
       $.get(url, function (data) { 
        $('#editor-container').modal('show'); 
        $('#editor-content-container').html(data); 
       }); 
      }); 

      $('#editor-container').on('hidden.bs.modal', function() { 
       $(this).removeData('bs.modal'); 
      }); 
     }); 

     function success(data, status, xhr) { 
      $('#editor-container').modal('hide'); 
     } 

     function failure(xhr, status, error) { 
      $('#editor-container').modal('show'); 
     } 
</script> 
} 

莫代爾PartialView(Edit.cshtml)

@using WebApplication1.Models; 

@model PersonViewModel 

<div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
     <span aria-hidden="true">&times;</span> 
    </button> 
    <h4 class="modal-title" id="editor-title">Edit Person</h4> 
</div> 

@using (Ajax.BeginForm("Edit", "Person", FormMethod.Post, new AjaxOptions 
{ 
    InsertionMode = InsertionMode.Replace, 
    HttpMethod = "POST", 
    UpdateTargetId = "editor-success-" + @Model.id, 
    OnSuccess = "success", 
    OnFailure = "failure", 
})) 
{ 
    @Html.ValidationSummary() 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.id) 
    <div class="modal-body"> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.name) 
      @Html.EditorFor(model => model.name) 
     </div> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.age) 
      @Html.EditorFor(model => model.age) 
     </div> 
    </div> 
    <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
     <button type="submit" class="btn btn-primary">Save</button> 
    </div> 
} 

列表項PartialView(ListItem.cshtml)

@using WebApplication1.Models; 

@model PersonViewModel 

@{ 
    var item = Model; 
} 

    <div class="row"> 
     <div class="col-md-5">@item.name</div> 
     <div class="col-md-5">@item.age</div> 
     <button type="button" class="btn editor-container" data-id="@item.id" 
       data-toggle="modal" data-target="#editor-container"> 
      Edit 
     </button> 
    </div> 

控制器(PersonController.cs)

public class PersonController : Controller 
    {   
     // GET: Person 
     public ActionResult Index() 
     { 
      return View(GetPersons()); 
     } 

     [HttpGet] 
     public ActionResult Edit(int id) 
     { 
      PersonViewModel p = GetPersons().Find(m => m.id == id); 

      return PartialView("Edit", p); 
     } 

     [HttpPost, ValidateAntiForgeryToken] 
     public ActionResult Edit(PersonViewModel p) 
     { 
      if (TryValidateModel(p)) 
      {     
       return PartialView("ListItem", p); 
      } 

      Response.StatusCode = 400; 

      return PartialView("Edit", p); 
     } 

     private List<PersonViewModel> GetPersons() 
     { 
      List<PersonViewModel> plist = new List<PersonViewModel>(); 

      PersonViewModel p = new PersonViewModel() 
      { 
       id = 1, 
       name = "Person1", 
       age = 33, 
      }; 

      plist.Add(p); 

      p = new PersonViewModel() 
      { 
       id = 2, 
       name = "Person2", 
       age = 30, 
      }; 

      plist.Add(p); 

      return plist; 
     } 
    } 

回答

2

的$不用彷徨已默認啓用緩存,使用$就代替。您可以在通話中禁用緩存。

var url = "/Person/Edit/" + pid; 
$.ajax({ 
    url: url, 
    success: function(data){ 
    $('#editor-container').modal('show'); 
    $('#editor-content-container').html(data); 
    }, 
    cache: false 
}); 
+0

感謝您的答覆。我試過了你的建議,但我仍然有同樣的問題。 –

1

我解決了我遇到的問題。我相信這是用按鈕將partialview加載到模態對話框中的。它在AJAX文章中被修改,因爲它位於用作updatetargetid的td元素內部的partialview中。我做了以下更改:

  1. 我從ListItem partialview中刪除按鈕並將其放置在父視圖(index.cshtml)中。
  2. 我刪除了父視圖中td元素上的id,並在我需要更新的字段周圍添加了div元素。我給了這個最初用於td元素的id。這個div是在AJAX文章的updatetargetid中使用的。

每次我點擊某個人的編輯按鈕時,現在在我的控制器中調用get動作,模態顯示正確人員的詳細信息。我添加了下面更新的代碼:

父視圖(索引。CSHTML)

@using WebApplication1.Models; 

@model List<PersonViewModel> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<div class="container"> 
    <table> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td> 
        <div class="row"> 
         <div id="[email protected]"> 
          @Html.Partial("ListItem", item) 
         </div> 
         <div class="col-md-4"> 
          <button type="button" class="btn editor-container" data-id="@item.id" 
            data-toggle="modal" data-target="#editor-container"> 
           Edit 
          </button> 
         </div> 
        </div> 
       </td> 
      </tr> 
     } 
    </table> 
</div> 

<div class="modal" id="editor-container" tabindex="-1" role="dialog" aria-labelledby="editor-title"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content" id="editor-content-container"></div> 
    </div> 
</div> 

@section scripts 
{ 
    <script type="text/javascript"> 
     $(function() { 
      $('.editor-container').click(function() { 
       var pid = $(this).attr('data-id'); 
       var url = "/Person/Edit/" + pid; 
       $.ajax({ 
        url: url, 
        cache: false, 
        success: function (data) { 
         $('#editor-content-container').html(data); 
         $('#editor-container').modal('show'); 
        } 
       }); 
      }); 

      $('#editor-container').on('hidden.bs.modal', function() { 
       $(this).removeData('bs.modal'); 
      }); 
     }); 

     function success(data, status, xhr) { 
      $('#editor-container').modal('hide'); 
     } 

     function failure(xhr, status, error) { 
      $('#editor-container').modal('show'); 
     } 
</script> 
} 

的ListItem partialview(ListItem.cshtml)

@using WebApplication1.Models; 

@model PersonViewModel 

@{ 
    var item = Model; 
} 

<div class="col-md-4">@item.name</div> 
<div class="col-md-4">@item.age</div>