Ideas
- Query the js file required for jQuery Template:
- Generate template content in <script type="text/x-jquery-tmpl" >/script> containing placeholders
- When clicking the Add button, add the template content to the interface and assign a value to the placeholder.
The content of jQuery Template is roughly like this:
<script type="text/x-jquery-tmpl" > <li style="padding-bottom:15px"> <input autocomplete="off" name="" type="hidden" value="${index}" /> <img src="/Content/images/" 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>
In order to obtain the above content, we obtain it from the helper method:
<script type="text/x-jquery-tmpl" > @("MovieEntryEditor", new Movie()) </script>
Help class CollectionEditingHtmlExtensions:
The template content is also generated through this partial view, except that the generated content contains placeholders.
using System; using ; using ; using ; using ; namespace { public static class CollectionEditingHtmlExtensions { /// <summary> /// The goal is to generate the following format ///<input autocomplete="off" name="" 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">name of collection attribute</param> /// <returns></returns> public static IDisposable BeginCollectionItem<TModel>(this HtmlHelper<TModel> html, string collectionName) { if ((collectionName)) { throw new ArgumentException("collectionName is null or empty","collectionName"); } string collectionIndexFieldName = ("{0}.Index", collectionName);// string itemIndex = null; if ((JQueryTemplatingEnabledKey)) { itemIndex = "${index}"; } else { itemIndex = GetCollectionItemIndex(collectionIndexFieldName); } //For example, FavouriteMovies[6d85a95b-1dee-4175-bfae-73fad6a3763b] string collectionItemName = ("{0}[{1}]", collectionName, itemIndex); TagBuilder indexField = new TagBuilder("input"); (new Dictionary<string, string>() { { "name", ("{0}.Index", collectionName) }, //name="" { "value", itemIndex },//value="6d85a95b-1dee-4175-bfae-73fad6a3763b" { "type", "hidden" }, { "autocomplete", "off" } }); (()); return new CollectionItemNamePrefixScope(, collectionItemName); } private class CollectionItemNamePrefixScope : IDisposable { private readonly TemplateInfo _templateInfo; private readonly string _previousPrefix; public CollectionItemNamePrefixScope(TemplateInfo templateInfo, string collectionItemName) { this._templateInfo = templateInfo; _previousPrefix = ; = collectionItemName; } public void Dispose() { _templateInfo.HtmlFieldPrefix = _previousPrefix; } } /// <summary> /// Think of the key, store the Guid string in the context /// If you add to a partial view, you will directly generate a Guid string /// If it is an update, in order to maintain consistency with ModelState, iterate over the original Guid /// </summary> /// <param name="collectionIndexFieldName"></param> /// <returns>Return Guid string</returns> private static string GetCollectionItemIndex(string collectionIndexFieldName) { Queue<string> previousIndices = (Queue<string>)[collectionIndexFieldName]; if (previousIndices == null) { [collectionIndexFieldName] = previousIndices = new Queue<string>(); string previousIndicesValues = [collectionIndexFieldName]; if (!(previousIndicesValues)) { foreach (string index in (',')) { (index); } } } return > 0 ? () : ().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); (JQueryTemplatingEnabledKey, true); return (partialViewName, modelDefaultValues, viewData); } } }
Partial view
@using @model <li style="padding-bottom: 15px;"> @using (("FavouriteMovies")) { <img src="@("~/Content/images/")" style="cursor: move" alt=""/> @(model => ) @(model => ) @(model => ) @(model => ) @(model => ) @(model => ) <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick=" $(this).parent().remove(); ">Delete the line</a> } </li>
HomeController
public ActionResult EditJqueryTemplate() { return View(CurrentUser); } [HttpPost] public ActionResult EditJqueryTemplate(User user) { if (!) { return View(user); } CurrentUser = user; return RedirectToAction("Display"); }
The complete code is as follows:
@using @using @model @{ = "EditJqueryTemplate"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>EditJqueryTemplate</h2> @using (()) { @(true) <fieldset> <legend>Favorite movies</legend> @(model => ) <div class="editor-label"> @(model => ) </div> <div class="editor-field"> @(model => ) @(model => ) </div> </fieldset> <fieldset> <legend>Favorite movies</legend> @if ( == null || == 0) { <p>No movie you like to watch~~</p> } <ul style="list-style-type: none"> @if ( != null) { foreach (Movie movie in ) { ("MovieEntryEditor", movie); } } </ul> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Add</a> </fieldset> <p> <input type="submit" value="submit" /> </p> } @section scripts { <script src="~/Scripts/"></script> <script type="text/x-jquery-tmpl" > @("MovieEntryEditor", new Movie()) </script> <script type="text/javascript"> $(function () { $("#movieEditor").sortable(); $('#addAnother').click(function() { (); }); }); var viewModel = { addNew: function () { $("#movieEditor").append($("#movieTemplate").tmpl({ index: viewModel._generateGuid() })); }, _generateGuid: function () { // Source: /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 = () * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return (16); }); } }; </script> }
This is all about this article about MVC using jQuery Template to achieve batch updates. I hope it will be helpful to everyone's learning and I hope everyone will support me more.