我有Resorts.aspx,它通過Ajax將ResortProducts.ascx的內容加載到div中。通過.ascx&ajax加載的ASP.NET MVC表單多次提交
Resorts.aspx<script type="text/JavaScript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
</script>
<script type="text/javascript">
$(function() {
// View Resort Rates >>
$('.ViewResortRatesLink').click(function() {
var sID = getQueryStringParameterByName("sID", this.href);
sID = sID.replace("~", "");
renderCart("div" + sID, "Loading Resort Product Information...");
$.ajax({
url: this.href,
success: function (result) {
renderCart("div" + sID, result)
}
});
return false;
});
});
function renderCart(container, data) {
$("#" + container).html(data);
}
這裏是ActionLink的代碼觸發上面的jQuery:
<%= Html.ActionLink(
"View Resort Rates >>",
"ResortProducts",
"Resorts",
new { sID = _supplier.SupplierID },
new { @class = "ViewResortRatesLink" })%>
這裏是div:的
<div id="div<% =ResortSupplierID %>"></div>
ResortProducts.ascx負載列表度假村產品,每種產品都包含自己的形式,可以將產品用於購物c藝術。
ResortProducts.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TrlSite.Services.ViewModels.ResortsView>" %>
<script type="text/JavaScript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
</script>
<script type="text/javascript">
$(function() {
//+ Add to cart
$(".addProductToCartForm").submit(function (e) {
e.preventDefault();
var HiddenCartForm = {
SupplierID: $(this.hSupplierID).val(),
Code: $(this.hProductCode).val(),
};
$.post($(this).attr("action"), HiddenCartForm, function (data) {
renderCart("rightColumn", data)
});
return false; // form already submitted using ajax, don't submit it again the regular way
});
});
function renderCart(container, data) {
$("#" + container).html(data);
}
</script>
<% foreach (var _supplier in Model.resorts) { %>
<table width="100%" cellpadding="2" cellspacing="0" id="tb_PAL">
<tr>
<td colspan="2" width="15%"> </td>
<td colspan="2">
<table width="100%" cellpadding="2" cellspacing="0" border="0" style="table-layout:fixed;">
<tr>
<td colspan="6"><hr /></td>
</tr>
<tr style="font-weight:bold;">
<td>ROOM TYPE</td>
<td> </td>
<td> </td>
<td align="center">PRICE</td>
</tr>
<tr>
<td colspan="6"><hr /></td>
</tr>
<%
foreach (var _product in products)
{ %>
<tr>
<td colspan="2" align="left" valign="top"><% = _product.Description %></td>
<td align="left">
<% using (Html.BeginForm("AddToCart", "ShoppingCart", FormMethod.Post, new { @class = "addProductToCartForm" }))
{ %>
<input type="hidden" name="hSupplierID" id="hSupplierID" value="<% = _supplier.SupplierID %>" />
<input type="hidden" name="hProductCode" id="hProductCode" value="<% = _product.Code %>" />
<input type="submit" value="+ Add to cart" />
<% } %>
</td>
<td valign="top" align="center">
<span id="roomprice_" onclick="return false;" style="text-decoration:none;cursor:default;color:#000000">
$<% = _product.TotalPrice %>
</span>
</td>
</tr>
<% } %>
<tr >
<td colspan="4">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="5"><hr /></td>
</tr>
</table>
<% } %>
可能有多個「景度假村價格>>」在Resorts.aspx鏈接,點擊每一個環節上會加載包含相應的產品列表ResortProducts.ascx。如果我只在Resorts.aspx中加載ResortProducts.ascx一次,然後單擊提交按鈕,一切正常。它張貼表單一次。但是,如果我多次加載.ascx,那麼表單將被提交次數.ascx被加載。我該如何解決這個問題?當我提交表單時,應該只發布一次,同時我應該能夠發佈多個表單。
我在看這個錯誤,我不相信我對我的回答是正確的,所以我刪除了它。對於那個很抱歉。我會繼續關注它,如果我有另一個想法,我會發布它。 – Dusty 2011-04-19 18:34:35
塵土飛揚,你的解決方案是將javascript移出.ascx文件。現在我擁有了一切工作。我將javascript移動到一個單獨的.js文件中,並將其引用到父.aspx文件中,並使用「jquery.live('click',function(e))」函數將.ascx文件生成的所有表單綁定到div所包含的父.aspx文件。我們失蹤的最後一部分是使用.live()函數綁定所有動態生成的表單。它現在按需要工作。感謝您的耐心和指導我朝着正確的方向發展。 – Alex 2011-04-19 19:18:55
好吧,如果其他人有類似的問題,我就不會回覆我的回答。我很高興你能工作。 – Dusty 2011-04-19 19:32:45