いつもお世話になっております。
ASP.NET MVCの部分ビューの動的更新について、皆様のお力をお貸しいただきたいです。
開発環境
OS : Windows10
.NET FrameWork : 4.5.2
Visual Studio : 2015 Professonal
Visual Studioプロジェクト : ASP.NET Web Application(.NET FrameWork)
プロジェクトテンプレート : MVCを指定
実行環境 : IIS(Visual Studioでの実行時)
Ajax.BeginFormを使用する為の参照設定 : NuGet => Microsoft.jQuery.Unobtrusive.Ajax 3.2.5
JQueryの読込も正しく行っております。
現在、以下のソースにて、問題が発生しております。
Test/Index.cshtml
cshtml
1<div> 2 @Html.Partial("_List", Model) 3</div>
Test/_List.cshtml
cshtml
1@using BootstrapSite1.Models.Test 2@model TestViewModel 3 4@using (Ajax.BeginForm("Delete", "Test", 5 new AjaxOptions 6 { 7 InsertionMode = InsertionMode.Replace, 8 HttpMethod = "POST", 9 UpdateTargetId = "system-list", 10 11 })) 12{ 13 <table> 14 <tr> 15 <th>id</th> 16 <th>name</th> 17 <th>削除</th> 18 </tr> 19 @for(int i = 0; i < Model.SystemList.Count; i++) 20 { 21 <tr> 22 <td> 23 @Html.DisplayFor(m=>m.SystemList[i].Id) 24 @Html.HiddenFor(m=>m.SystemList[i].Id) 25 </td> 26 <td>@Html.TextBoxFor(m=>m.SystemList[i].Name)</td> 27 <td> 28 <button type="submit" name="idx" value="@i">削除</button> 29 </td> 30 </tr> 31 } 32 </table> 33} 34``` 35 36Model 37```C# 38using System.Collections.Generic; 39using System.Data; 40using System.Text; 41 42namespace BootstrapSite1.Models.Test 43{ 44 public class SystemModel 45 { 46 public int Id { get; set; } 47 48 public string Name { get; set; } 49 50 } 51 public class TestViewModel 52 { 53 public List<SystemModel> SystemList { get; set; } 54 55 public TestViewModel () 56 { 57 this.SystemList = new List<SystemModel>(); 58 } 59 60 public void SetSystemList() 61 { 62 this.SystemList.Add(new SystemModel { Id = 1, Name = "1" }); 63 this.SystemList.Add(new SystemModel { Id = 2, Name = "2" }); 64 this.SystemList.Add(new SystemModel { Id = 3, Name = "3" }); 65 } 66}
Controller
C#
1using System.Collections.Generic; 2using System.Linq; 3using System.Web.Mvc; 4 5namespace BootstrapSite1.Controllers 6{ 7 public class TestController: Controller 8 { 9 public ActionResult Index() 10 { 11 TestViewModel model = new TestViewModel (); 12 model.SetSystemList(); 13 return View(model); 14 } 15 16 public PartialViewResult Delete(TestViewModel model, int idx) 17 { 18 List<SystemModel> list = model.SystemList; 19 list.RemoveAt(idx); 20 model.SystemList = list; 21 return PartialView("_List", model); 22 } 23 } 24}
上記のソースを実行した場合、画面には以下の表が表示されます。
id | name | 削除 |
---|---|---|
1 | 1 | 削除 |
2 | 2 | 削除 |
3 | 3 | 削除 |
しかし、1行目の削除ボタンを実行するした場合
id | name | 削除 |
---|---|---|
2 | 1 | 削除 |
3 | 3 | 削除 |
上記の様に、部分的に再描画されますが、
@Html.HiddenForまたはTextBoxForなど出力している箇所が、前回の値を参照してしまっているような状態です。
部分ビュー出力時にデバックで追いかけても前回データはModelから削除されている為、前回値が表示されるのが何故か理解できません。
どうすれば、ビューが正しく更新されますでしょうか。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー