<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
<script type="text/x-jquery-tmpl" id="movieTemplate"> <li style="padding-bottom:15px"> <input autocomplete="off" name="FavouriteMovies.Index" type="hidden" value="${index}" /> <img src="/Content/images/draggable-icon.png" style="cursor: move" alt=""/> <label>Title</label> <input name="FavouriteMovies[${index}].Title" type="text" value="" /> <label>Rating</label> <input name="FavouriteMovies[${index}].Rating" type="text" value="0" /> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="$(this).parent().remove();">Delete</a> </li> </script>
為了得到以上內容,由幫助類方法獲得:
<script type="text/x-jquery-tmpl" id="movieTemplate"> @Html.CollectionItemJQueryTemplate("MovieEntryEditor", new Movie()) </script>
模版內容同樣是通過MovieEntryEditor.cshtml這個部分檢視生成的,只不過生成的內容中包含了預留位置。
using System; using System.Collections.Generic; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace VariableCollection.Extension { public static class CollectionEditingHtmlExtensions { /// <summary> /// 目標是生成如下格式 ///<input autocomplete="off" name="FavouriteMovies.Index" type="hidden" value="6d85a95b-1dee-4175-bfae-73fad6a3763b" /> ///<label>Title</label> ///<input class="text-box single-line" name="FavouriteMovies[6d85a95b-1dee-4175-bfae-73fad6a3763b].Title" type="text" value="Movie 1" /> ///<span class="field-validation-valid"></span> /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="html"></param> /// <param name="collectionName">集合屬性的名稱</param> /// <returns></returns> public static IDisposable BeginCollectionItem<TModel>(this HtmlHelper<TModel> html, string collectionName) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentException("collectionName is null or empty","collectionName"); } string collectionIndexFieldName = String.Format("{0}.Index", collectionName);//FavouriteMovies.Index string itemIndex = null; if (html.ViewData.ContainsKey(JQueryTemplatingEnabledKey)) { itemIndex = "${index}"; } else { itemIndex = GetCollectionItemIndex(collectionIndexFieldName); } //比如,FavouriteMovies[6d85a95b-1dee-4175-bfae-73fad6a3763b] string collectionItemName = string.Format("{0}[{1}]", collectionName, itemIndex); TagBuilder indexField = new TagBuilder("input"); indexField.MergeAttributes(new Dictionary<string, string>() { { "name", String.Format("{0}.Index", collectionName) }, //name="FavouriteMovies.Index" { "value", itemIndex },//value="6d85a95b-1dee-4175-bfae-73fad6a3763b" { "type", "hidden" }, { "autocomplete", "off" } }); html.ViewContext.Writer.WriteLine(indexField.ToString(TagRenderMode.SelfClosing)); return new CollectionItemNamePrefixScope(html.ViewData.TemplateInfo, collectionItemName); } private class CollectionItemNamePrefixScope : IDisposable { private readonly TemplateInfo _templateInfo; private readonly string _previousPrefix; public CollectionItemNamePrefixScope(TemplateInfo templateInfo, string collectionItemName) { this._templateInfo = templateInfo; _previousPrefix = templateInfo.HtmlFieldPrefix; templateInfo.HtmlFieldPrefix = collectionItemName; } public void Dispose() { _templateInfo.HtmlFieldPrefix = _previousPrefix; } } /// <summary> /// 以FavouriteMovies.Index為鍵,把Guid字串存放在上下文中 /// 如果是新增進入部分檢視,就直接生成一個Guid字串 /// 如果是更新,為了保持和ModelState的一致,就遍歷原先的Guid /// </summary> /// <param name="collectionIndexFieldName">FavouriteMovies.Index</param> /// <returns>返回Guid字串</returns> private static string GetCollectionItemIndex(string collectionIndexFieldName) { Queue<string> previousIndices = (Queue<string>)HttpContext.Current.Items[collectionIndexFieldName]; if (previousIndices == null) { HttpContext.Current.Items[collectionIndexFieldName] = previousIndices = new Queue<string>(); string previousIndicesValues = HttpContext.Current.Request[collectionIndexFieldName]; if (!string.IsNullOrWhiteSpace(previousIndicesValues)) { foreach (string index in previousIndicesValues.Split(',')) { previousIndices.Enqueue(index); } } } return previousIndices.Count > 0 ? previousIndices.Dequeue() : Guid.NewGuid().ToString(); } private const string JQueryTemplatingEnabledKey = "__BeginCollectionItem_jQuery"; public static MvcHtmlString CollectionItemJQueryTemplate<TModel, TCollectionItem>(this HtmlHelper<TModel> html, string partialViewName, TCollectionItem modelDefaultValues) { ViewDataDictionary<TCollectionItem> viewData = new ViewDataDictionary<TCollectionItem>(modelDefaultValues); viewData.Add(JQueryTemplatingEnabledKey, true); return html.Partial(partialViewName, modelDefaultValues, viewData); } } }
@using VariableCollection.Extension @model VariableCollection.Models.Movie <li style="padding-bottom: 15px;"> @using (Html.BeginCollectionItem("FavouriteMovies")) { <img src="@Url.Content("~/Content/images/draggable-icon.png")" style="cursor: move" alt=""/> @Html.LabelFor(model => model.Title) @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) @Html.LabelFor(model => model.Rating) @Html.EditorFor(model => model.Rating) @Html.ValidationMessageFor(model => model.Rating) <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick=" $(this).parent().remove(); ">刪除行</a> } </li>
public ActionResult EditJqueryTemplate() { return View(CurrentUser); } [HttpPost] public ActionResult EditJqueryTemplate(User user) { if (!this.ModelState.IsValid) { return View(user); } CurrentUser = user; return RedirectToAction("Display"); }
@using VariableCollection.Extension @using VariableCollection.Models @model VariableCollection.Models.User @{ ViewBag.Title = "EditJqueryTemplate"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>EditJqueryTemplate</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>最喜歡看的電影</legend> @Html.HiddenFor(model => model.Id) <div class="editor-label"> @Html.LabelFor(model => model.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> </fieldset> <fieldset> <legend>最喜歡看的電影</legend> @if (Model.FavouriteMovies == null || Model.FavouriteMovies.Count == 0) { <p>沒有喜歡看的電影~~</p> } <ul id="movieEditor" style="list-style-type: none"> @if (Model.FavouriteMovies != null) { foreach (Movie movie in Model.FavouriteMovies) { Html.RenderPartial("MovieEntryEditor", movie); } } </ul> <a id="addAnother" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新增</a> </fieldset> <p> <input type="submit" value="提交" /> </p> } @section scripts { <script src="~/Scripts/jquery.tmpl.min.js"></script> <script type="text/x-jquery-tmpl" id="movieTemplate"> @Html.CollectionItemJQueryTemplate("MovieEntryEditor", new Movie()) </script> <script type="text/javascript"> $(function () { $("#movieEditor").sortable(); $('#addAnother').click(function() { viewModel.addNew(); }); }); var viewModel = { addNew: function () { $("#movieEditor").append($("#movieTemplate").tmpl({ index: viewModel._generateGuid() })); }, _generateGuid: function () { // Source: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/105074#105074 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } }; </script> }
到此這篇關於ASP.NET MVC使用jQuery Template實現批次更新的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45