一個潛在的策略是將HTML僅用於一個模式對話框,並且在某種JavaScript數據結構中具有所有字符串的所有長版本,如散列數組或其他類型。這樣可以節省您的頁面被數百個模態對話框的HTML膨脹
然後,當用戶單擊「顯示長版本」鏈接時 - 您有一些查詢將用模式對話框替換模式對話框中的內部文本它從哈希數組中檢索到的字符串的適當「長版本」,然後顯示該對話框。
如果你想獲得更多的信息 - 你只能在頁面中加載短版本的字符串,並且當用戶點擊鏈接時,它會通過AJAX檢索字符串的'長版'。
Twitter Bootstrap帶有模式對話框,參見here,JQuery有一些將HTML插入元素的方法 - 請參見here。
如果您查看Twitter Bootstrap模式對話框的文檔,您會發現它可以選擇使用remote:
選項通過AJAX遠程加載數據。
這裏是你能如何利用Twitter的引導的一個粗略的例子,通過AJAX
做到這一點
控制器
public ActionResult Index()
{
var model = GetLongStrings();
return View(model);
}
public ActionResult FindLongString(string shortString)
{
var longStrings = GetLongStrings();
var longStringToReturn = longStrings
.FirstOrDefault(x => x.StartsWith(shortString));
return Content(longStringToReturn);
}
然後,您認爲會是這個樣子
@foreach (var result in results)
{
var shortString = result.Substring(0, 5);
<section>
<div class="row-fluid">
<div class="span12">@shortString</div>
@Html.ActionLink("See long version", "FindLongString",
new { shortString = shortString },
new { data_toggle = "modal",
data_target = "#myModal",
aria_hidden = "true" })
</div>
</section>
}
<div id="myModal" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
</div>
</div>
ActionLink中的數據屬性非常重要,因爲它們會指示Twitter Bootstrap使用哪些選項用於對話。
如果每個字符串都有一個ID,則代碼示例可以得到改進 - FindLongString
可以接受一個int作爲參數,這意味着您可以在ActionLink中傳遞此代碼。
我不認爲你的意思是串聯:http://en.wikipedia.org/wiki/Concatenation – SoonDead 2013-03-26 20:22:10
「Lunchy的對話建議」是可能是指由於某種原因刪除的評論。 – SoonDead 2013-03-26 20:26:49
@SoonDead:謝謝。是的,我不知道我爲什麼寫連接。我知道這個詞的意思。 :) – doosh 2013-03-26 22:54:08