ASP.Net MVC 3の表形式データのインライン編集

ASP.Net MVC 3でのAjaxGridの別の実装を紹介します。

この記事では、インライン編集、ajaxソート、ajaxページャーを使用して表形式を作成する方法について説明します。



この実装は、いくつかの利用可能なコンポーネントのコンパイルです。

通常そうであるように、運用上のニーズのために、編集可能な表データを表示する必要がありました。 スクリーンショットのように:



望ましい結果



始める前に、AjaxGridScaffolderをインストールします

PM> Install-Package AjaxGridScaffolder











データレイヤーを作成する





エッセンス。

 namespace HabrahabrMVC.Domain.Entities { public class RealtyObject { public int Id { get; set; } public string City { get; set; } public string Street { get; set; } public string House { get; set; } public double Cost { get; set; } public bool Visible { get; set; } } }
      
      





インターフェース

 namespace HabrahabrMVC.Domain.Abstract { public interface IRepository { IQueryable<RealtyObject> GetAll(); void Save(RealtyObject objectToSave); } }
      
      





コンテキスト。

 namespace HabrahabrMVC.Domain.Concrete { public class EFContext : DbContext { public DbSet<RealtyObject> RealtyObjects { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } } }
      
      





インターフェイスの実装。

 namespace HabrahabrMVC.Domain.Concrete { public class EFRepository :IRepository { EFContext _db = new EFContext(); public void Save(Entities.RealtyObject objectToSave) { _db.Entry(_db.RealtyObjects.SingleOrDefault(z=>z.Id==objectToSave.Id)).CurrentValues.SetValues(objectToSave); _db.Entry<RealtyObject>(_db.RealtyObjects.SingleOrDefault(z => z.Id == objectToSave.Id)).State = System.Data.EntityState.Modified; _db.SaveChanges(); } public IQueryable<RealtyObject> GetAll() { return _db.RealtyObjects.AsQueryable(); } } }
      
      





コントローラーを作成する



ObjectsView:

コントローラー作成

1)このためには、ControllersフォルダーのObject Explorer'eを右クリックし、Add-> Controller ...(Add-> Controller ...)を選択します。

2)ObjectsViewControllerという名前を書きます。

3)テンプレート-Ajax Grid Controller

4)モデルクラス-RealtyObject(HabraHabrMVC3.Domain.Entities)

5)データコンテキストクラス-EFContext(HabraHabrMVC3.Domain.Concrete)

6)追加。



ウィザードがコードを生成した後、コントローラーにコンストラクターを追加して、Ninjectがデータレイヤーを正しく挿入できるようにします。

 //private EFContext db = new EFContext(); private IRepository db; public ObjectsViewController(IRepository dbparam) { db = dbparam; }
      
      







次に、生成されたコードのデータソースを変更する必要があります。

db.GetAll上のdb.RealtyObjects()

db.RealAllObjects.Find(id)on db.GetAll()。SingleOrDefault(z => z.Id == id)



データを作成、編集、削除するためのアクションは必要ありません。削除します。

また、GridData.cshtmlビューでは、編集ボタンと削除ボタンが削除されました。



この行は機能しません:

 ObjectQuery<RealtyObject> realtyobjects = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<RealtyObject>();
      
      





なぜなら dbはIObjectContextAdapterをサポートしません。

したがって、Linq拡張メソッドを追加します。



別の静的クラスOrderByHelperを作成しました。

 namespace HabraHabrMVC3.Infrastructure { public static class OrderByHelper { public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> enumerable, string orderBy) { return enumerable.AsQueryable().OrderBy(orderBy).AsEnumerable(); } public static IQueryable<T> OrderBy<T>(this IQueryable<T> collection, string orderBy) { foreach (OrderByInfo orderByInfo in ParseOrderBy(orderBy)) collection = ApplyOrderBy<T>(collection, orderByInfo); return collection; } private static IQueryable<T> ApplyOrderBy<T>(IQueryable<T> collection, OrderByInfo orderByInfo) { string[] props = orderByInfo.PropertyName.Split('.'); Type type = typeof(T); ParameterExpression arg = Expression.Parameter(type, "x"); Expression expr = arg; foreach (string prop in props) { // use reflection (not ComponentModel) to mirror LINQ PropertyInfo pi = type.GetProperty(prop); expr = Expression.Property(expr, pi); type = pi.PropertyType; } Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type); LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg); string methodName = String.Empty; if (!orderByInfo.Initial && collection is IOrderedQueryable<T>) { if (orderByInfo.Direction == SortDirection.Ascending) methodName = "ThenBy"; else methodName = "ThenByDescending"; } else { if (orderByInfo.Direction == SortDirection.Ascending) methodName = "OrderBy"; else methodName = "OrderByDescending"; } //TODO: apply caching to the generic methodsinfos? return (IOrderedQueryable<T>)typeof(Queryable).GetMethods().Single( method => method.Name == methodName && method.IsGenericMethodDefinition && method.GetGenericArguments().Length == 2 && method.GetParameters().Length == 2) .MakeGenericMethod(typeof(T), type) .Invoke(null, new object[] { collection, lambda }); } private static IEnumerable<OrderByInfo> ParseOrderBy(string orderBy) { if (String.IsNullOrEmpty(orderBy)) yield break; string[] items = orderBy.Split(','); bool initial = true; foreach (string item in items) { string[] pair = item.Trim().Split(' '); if (pair.Length > 2) throw new ArgumentException(String.Format("Invalid OrderBy string '{0}'. Order By Format: Property, Property2 ASC, Property2 DESC", item)); string prop = pair[0].Trim(); if (String.IsNullOrEmpty(prop)) throw new ArgumentException("Invalid Property. Order By Format: Property, Property2 ASC, Property2 DESC"); SortDirection dir = SortDirection.Ascending; if (pair.Length == 2) dir = ("desc".Equals(pair[1].Trim(), StringComparison.OrdinalIgnoreCase) ? SortDirection.Descending : SortDirection.Ascending); yield return new OrderByInfo() { PropertyName = prop, Direction = dir, Initial = initial }; initial = false; } } private class OrderByInfo { public string PropertyName { get; set; } public SortDirection Direction { get; set; } public bool Initial { get; set; } } private enum SortDirection { Ascending = 0, Descending = 1 } } }
      
      







そして、新しい種類のアクションGridData:



 public ActionResult GridData(int start = 0, int itemsPerPage = 20, string orderBy = "Id", bool desc = false) { Response.AppendHeader("X-Total-Row-Count", db.GetAll().Count().ToString()); var realtyobjects = db.GetAll().OrderBy(orderBy + (desc ? " desc" : "")); return PartialView(realtyobjects.Skip(start).Take(itemsPerPage)); }
      
      







これで、ソートとページングを備えた素晴らしいAjaxテーブルができました。



インライン編集の追加





HTMLヘルパー専用の別の拡張メソッドを追加します。



 namespace System.Web { public static class HtmlPrefixScopeExtensions { private const string idsToReuseKey = "__htmlPrefixScopeExtensions_IdsToReuse_"; public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName) { var idsToReuse = GetIdsToReuse(html.ViewContext.HttpContext, collectionName); string itemIndex = idsToReuse.Count > 0 ? idsToReuse.Dequeue() : Guid.NewGuid().ToString(); // autocomplete="off" is needed to work around a very annoying Chrome behaviour whereby it reuses old values after the user clicks "Back", which causes the xyz.index and xyz[...] values to get out of sync. html.ViewContext.Writer.WriteLine(string.Format("<input type=\"hidden\" name=\"{0}.index\" autocomplete=\"off\" value=\"{1}\" />", collectionName, html.Encode(itemIndex))); return BeginHtmlFieldPrefixScope(html, string.Format("{0}[{1}]", collectionName, itemIndex)); } public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix) { return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix); } private static Queue<string> GetIdsToReuse(HttpContextBase httpContext, string collectionName) { // We need to use the same sequence of IDs following a server-side validation failure, // otherwise the framework won't render the validation error messages next to each item. string key = idsToReuseKey + collectionName; var queue = (Queue<string>)httpContext.Items[key]; if (queue == null) { httpContext.Items[key] = queue = new Queue<string>(); var previouslyUsedIds = httpContext.Request[collectionName + ".index"]; if (!string.IsNullOrEmpty(previouslyUsedIds)) foreach (string previouslyUsedId in previouslyUsedIds.Split(',')) queue.Enqueue(previouslyUsedId); } return queue; } private class HtmlFieldPrefixScope : IDisposable { private readonly TemplateInfo templateInfo; private readonly string previousHtmlFieldPrefix; public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix) { this.templateInfo = templateInfo; previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix; templateInfo.HtmlFieldPrefix = htmlFieldPrefix; } public void Dispose() { templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix; } } } }
      
      







ビューGridDataを置き換えます

 @model IEnumerable<HabraHabrMVC3.Domain.Entities.RealtyObject> @if (Model.Count() > 0) { foreach (var item in Model) { <text>@Html.Partial("Edit", item)</text> } }
      
      







編集の表示:

 @model HabraHabrMVC3.Domain.Entities.RealtyObject @using (Html.BeginCollectionItem("objects")) { <tr> <td> @Html.EditorFor(model => model.City) @Html.ValidationMessageFor(model => model.City) </td> <td> @Html.EditorFor(model => model.Street) @Html.ValidationMessageFor(model => model.Street) </td> <td> @Html.EditorFor(model => model.House) @Html.ValidationMessageFor(model => model.House) </td> <td> @Html.EditorFor(model => model.Cost) @Html.ValidationMessageFor(model => model.Cost) </td> <td> @Html.EditorFor(model => model.Visible) @Html.ValidationMessageFor(model => model.Visible) </td> </tr> }
      
      







ビューインデックスに以下を追加します。

 @using (Html.BeginForm("Save", "ObjectsView", FormMethod.Post)) { <table id="AjaxGrid"> ...code... </table> <input type="submit" id="save" value="" /> }
      
      







アクションの保存を追加:

 [HttpPost] public ActionResult Save(ICollection<RealtyObject> objects) { foreach(var item in objects) { db.Save(item); } return RedirectToAction("Index"); }
      
      





ここでは、@ usingのコレクション名(Html.BeginCollectionItem( " オブジェクト "))とアクションSave(ICollection オブジェクト )メソッドのパラメーター名が一致することが重要です。



それだけで、インライン編集、ajaxソート、ajaxページャーを備えた表形式になりました。



インターネットで使用されている文献のリスト:



1) Ajax Grid Scaffolder



2) 可変長リストの編集、ASP.NET MVC 2スタイル



3) 動的SQLのようなLinq OrderBy拡張



結果をダウンロードします。



All Articles