我有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