通过.ascx和ajax加载的ASP.NET MVC表单提交多次
||
我有Resorts.aspx,它通过Ajax将来自ResortProducts.ascx的内容加载到div中。
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);
}
这是在jQuery上方触发的ActionLink的代码:
<%= Html.ActionLink(
\"View Resort Rates >>\",
\"ResortProducts\",
\"Resorts\",
new { sID = _supplier.SupplierID },
new { @class = \"ViewResortRatesLink\" })%>
这是Div:
<div id=\"div<% =ResortSupplierID %>\"></div>
ResortProducts.ascx加载Resort产品列表,每个产品包含其自己的形式,可以使用该形式将产品添加到购物车。
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,则该表单将提交提交的次数。我该如何解决这个问题?当我提交表单时,应该只发布一次,同时我应该能够发布多个表单。
没有找到相关结果
已邀请:
2 个回复
钾涎净介
艰管垮淮