網(wǎng)站首頁 編程語言 正文
思路
- 引用jQuery Template所需要的js文件:jquery.tmpl.min.js
- 在<script type="text/x-jquery-tmpl" id="movieTemplate"></script>中生成模版內(nèi)容,里面包含占位符
- 點擊添加按鈕的時候,把模版內(nèi)容追加到界面上,并給占位符賦值
jQuery Template的內(nèi)容大致是這樣:
<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="#" onclick="$(this).parent().remove();">Delete</a>
</li>
</script>
為了得到以上內(nèi)容,由幫助類方法獲得:
<script type="text/x-jquery-tmpl" id="movieTemplate">
@Html.CollectionItemJQueryTemplate("MovieEntryEditor", new Movie())
</script>
幫助類CollectionEditingHtmlExtensions:
模版內(nèi)容同樣是通過MovieEntryEditor.cshtml這個部分視圖生成的,只不過生成的內(nèi)容中包含了占位符。
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>
/// 目標(biāo)是生成如下格式
///<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);
}
//比如,F(xiàn)avouriteMovies[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字符串存放在上下文中
/// 如果是添加進(jìn)入部分視圖,就直接生成一個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);
}
}
}
MovieEntryEditor.cshtm部分視圖
@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="#" onclick=" $(this).parent().remove(); ">刪除行</a>
}
</li>
HomeController
public ActionResult EditJqueryTemplate()
{
return View(CurrentUser);
}
[HttpPost]
public ActionResult EditJqueryTemplate(User user)
{
if (!this.ModelState.IsValid)
{
return View(user);
}
CurrentUser = user;
return RedirectToAction("Display");
}
EditJqueryTemplate.cshtml完整代碼如下:
@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="#" >添加</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>
}
原文鏈接:https://www.cnblogs.com/darrenji/p/3717770.html
相關(guān)推薦
- 2022-07-07 Pandas提高數(shù)據(jù)分析效率的13個技巧匯總_python
- 2022-03-31 C#實現(xiàn)單位換算器_C#教程
- 2023-11-11 flask python 設(shè)置定時任務(wù) flask 周期性執(zhí)行任務(wù)方案
- 2022-06-19 rsa詳解及例題及python算法_python
- 2022-10-04 一文教會你用Python讀取PDF文件_python
- 2022-11-17 C#?函數(shù)返回多個值的方法詳情_C#教程
- 2023-01-15 使用tf.keras.MaxPooling1D出現(xiàn)錯誤問題及解決_python
- 2022-08-05 springBoot集成swagger啟動報錯:Failed to start bean ‘docu
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支