Như tôi vẫn còn mới để MVC 3 và jquery, tôi muốn biết một giải pháp thực hành tốt nhất để như thế nào sau đây có thể được giải quyết:MVC 3, tái sử dụng của quang cảnh một phần và jquery, không mâu thuẫn DOM
tôi có một cái nhìn, nơi tôi sử dụng jquery ajax để lấy và hiển thị một phần xem với một số chi tiết sản phẩm cho sản phẩm A. Khung nhìn từng phần được nạp bao gồm một chuỗi mã html và jquery, được gắn với id được định nghĩa trong khung nhìn một phần.
Do đó, tôi muốn sử dụng lại chế độ xem một phần để hiển thị chi tiết từ các sản phẩm khác trên cùng một Chế độ xem (ví dụ: hiển thị chi tiết sản phẩm B trong hộp thoại bật lên). Bất cứ khi nào cửa sổ bật lên được hiển thị, chế độ xem một phần mới được tìm nạp sẽ xung đột với chế độ xem từng phần cho sản phẩm A, vì cùng một id được sử dụng trong html.
Có cách nào để đóng gói html và javascript trong giao diện phần, và tái sử dụng nó nhiều trang mà không cần lo lắng về bất kỳ cuộc xung đột với ID và các công cụ?
Tôi hy vọng câu hỏi của tôi có ý nghĩa. Cảm ơn,
/Nima
CẬP NHẬT
Dưới đây là một số mã giả, phác thảo vấn đề của tôi:
XEM
<script type="text/javascript">
$(document).ready(function() {
$('.productItems').click(function() {
var input = { productId: $(this).attr('data-productID') };
var url = url = '<%: Url.Content("~/ProductDetails/ShowProductDetails") %>';
// Show the modal box with product details
$('#dialogBox').dialog({
title: $(this).attr('data-productTitle')
});
// Fetch content in the background
$.get(url, input, function (result, response) {
$('#dialogBox').html(result);
});
});
});
</script>
<div id="detailsArea">
<% Html.RenderPartial("ProductDetails", Model.Product); %>
</div>
<div id="productLinks">
<span class="productItems" data-productID="123">Product B</a>
</div>
<div id="dialogBox" style="display: none;"></div>
Controller -> Hành động (ShowProductDetails)
public ActionResult ShowProductDetails(int productId)
{
// Get product from db. and return the partial view
return PartialView("ProductDetails", p);
}
Partial View (ProductDetails)
<script type="text/javascript">
function SetProductTabContent(selectedTab) {
$("#productDescriptionContent > div").css('display', 'none');
switch (selectedTab) {
case '#tab-1':
$('#productDescriptionText').css('display', 'block');
break;
case '#tab-2':
$('#productSpecificationText').css('display', 'block');
break;
}
$(document).ready(function() {
// Get all the menu items
var menuItems = $("#productMenu a");
// Select the first tab as default
menuItems.first().addClass("menuItemActive");
// Handle the look of the tabs, when user selects one.
menuItems.click(function() {
var item = $(this);
// Get content for the selected tab
SetProductTabContent(item.attr('href'));
menuItems.removeClass("menuItemActive");
item.addClass("menuItemActive");
return false;
});
});
</script>
<div id="productMenu" style="">
<a href="#tab-1">
<div class="menuItemHeader">Menu1</div>
</a>
<a href="#tab-2">
<div class="menuItemHeader">Menu2 </div>
</a>
</div>
<div id="productDescriptionContent">
<div id="productDescriptionText" style="display: none;">
<%: Model.Product.Description %>
</div>
<div id="productSpecificationText" style="display: none;">
<%: Model.Product.Description2%>
</div>
</div>
CẤP Khi xem một phần được nạp hai lần trong DOM, các divs xung đột.
gì về cách hiển thị nội dung của DialogBox trong iframe không? – Nima