-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using FeedbackMessages.Extensions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using WebApp.MVC.Models; | ||
|
||
namespace WebApp.MVC.Controllers | ||
{ | ||
/// <summary> | ||
/// Ajax系サンプル | ||
/// </summary> | ||
public class AjaxExampleController : Controller | ||
{ | ||
|
||
private List<ListExampleModel.User> UserList | ||
{ | ||
get | ||
{ | ||
return (List<ListExampleModel.User>)Session["AjaxExampleController.UserList"]; | ||
} | ||
set | ||
{ | ||
Session["AjaxExampleController.UserList"] = value; | ||
} | ||
} | ||
|
||
// GET: AjaxExample | ||
[HttpGet] | ||
public ActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
/// <summary> | ||
/// ユーザーリストビューを描画します。 | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public ActionResult PartialUserListView() | ||
{ | ||
Thread.Sleep(1000 * 3); | ||
|
||
if (UserList == null || UserList.Count == 0) | ||
{ | ||
UserList = new List<ListExampleModel.User> | ||
{ | ||
new ListExampleModel.User() | ||
{ | ||
UserId = "001", | ||
UserName = "ユーザーA", | ||
Age = 20 | ||
}, | ||
new ListExampleModel.User() | ||
{ | ||
UserId = "002", | ||
UserName = "ユーザーB", | ||
Age = 30 | ||
}, | ||
new ListExampleModel.User() | ||
{ | ||
UserId = "003", | ||
UserName = "ユーザーC", | ||
Age = 40 | ||
} | ||
}; | ||
} | ||
|
||
|
||
var myViewData = new ViewDataDictionary(ViewData); | ||
// WebApp.MVC.Models.ListExampleModel.UserList とマッピングできるようにプレフィックス指定 | ||
myViewData.TemplateInfo.HtmlFieldPrefix = "UserList"; | ||
|
||
myViewData.Model = UserList; | ||
|
||
return new PartialViewResult | ||
{ | ||
ViewData = myViewData, | ||
ViewName = "_UserList", | ||
TempData = TempData | ||
}; | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Postリクエストを処理します。 | ||
/// </summary> | ||
/// <param name="model"></param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public ActionResult Index(WebApp.MVC.Models.ListExampleModel model) | ||
{ | ||
|
||
foreach (var user in model.UserList) | ||
{ | ||
this.InfoMessage(user.UserId + ", " + user.UserName + ", " + user.Age); | ||
} | ||
|
||
// 編集内容キープ | ||
UserList = model.UserList; | ||
|
||
return View(model); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
namespace WebApp.MVC.Models | ||
{ | ||
[Serializable] | ||
public class ListExampleModel | ||
{ | ||
public class User { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@{ | ||
ViewBag.Title = "List Example"; | ||
} | ||
@model WebApp.MVC.Models.ListExampleModel | ||
|
||
@using (Html.BeginForm()) | ||
{ | ||
<script> | ||
$(function () { | ||
// Controller UserListメソッドでパーシャルビューを返す | ||
$.ajax({ | ||
type: "POST", | ||
url: "/AjaxExample/PartialUserListView", | ||
data: $(this).serialize(), | ||
success: function (data) { | ||
// パーシャルビューをセット | ||
$("#userListLoader").css("display", "none"); | ||
$("#userList").html(data); | ||
}, | ||
error: function (jqXHR, status, error) { | ||
console.error(error); | ||
console.error(status); | ||
} | ||
}); | ||
}); | ||
</script> | ||
<div style="padding:1em;"> | ||
<div id="userListLoader"> | ||
<div class="d-flex justify-content-center"> | ||
<div class="spinner-border" role="status"> | ||
<span class="visually-hidden">Loading...</span> | ||
</div> | ||
</div> | ||
</div> | ||
<div id="userList"></div> | ||
</div> | ||
|
||
<input type="submit" value="送信" class="btn btn-primary" /> | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@using System.Collections.Generic; | ||
@model List<WebApp.MVC.Models.ListExampleModel.User> | ||
|
||
<table class="table table-bordered table-sm table-hover"> | ||
<thead class="table-light"> | ||
|
||
<tr> | ||
<th>ID</th> | ||
<th>名前</th> | ||
<th>年齢</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
@for (var i = 0; i < Model.Count; i++) | ||
{ | ||
<tr> | ||
<td>@Html.TextBoxFor(m => m[i].UserId, new { @class = "form-control" })</td> | ||
<td>@Html.TextBoxFor(m => m[i].UserName, new { @class = "form-control" })</td> | ||
<td>@Html.TextBoxFor(m => m[i].Age, new { @class = "form-control" })</td> | ||
</tr> | ||
} | ||
|
||
</tbody> | ||
</table> |