我需要将隐藏字段中的值添加到我的Ajax帖子中。
我担心我对Ajax了解不多,所以我会尝试解释我需要的东西:
在我的视图页面中,我有以下内容:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="OneBySevenBin2">
<div class="OneBySevenBin2middle">
<div class="divTitleArea">
<p>
</p>
</div>
<ul>
<% if (Model.ProductNotFound)
{ %>
<!-- List the retailers as we have none in stock -->
<%= Html.WidgetProductNotFoundList(Model) %>
<% }
else
{ %>
<% using (Html.BeginForm())
{%>
<%= Html.AntiForgeryToken() %>
<%= Html.WidgetProductFoundListThreeStageBin2(Model)%>
<div class="stepFour" data-bind="stopBinding: true">
<% if (string.IsNullOrEmpty(ViewBag.Confirmation))
{
Ajax.BeginForm("OneBySevenBin2", new AjaxOptions()
{
HttpMethod = "POST"
});
{ %>
<div id="uandp">
<p>
<input type="text" name="username" class="txtUsername" id="email" value="Username" />
<%= Html.ValidationMessage("Email", "Please enter a valid Email") %>
</p>
<p>
<input type="password" name="password" class="txtPassword" value="Password" />
</p>
<input type="hidden" id="hiddenVal" class="hiddenVal" name="hiddenVal" value="1" />
</div>
<div id="rightaddtocart">
<div id="divaddtocartbtn">
<input type="image" class="buynow" id="imgaddtocart" alt="Add to Cart" src="http://static.e-talemedia.net/content/images/addtobasketbutton.png" />
</div>
</div>
<% } %>
<% }
else
{ %>
<p>
<%-- <%= ViewBag.Confirmation %>--%>
<img class="divAdded" src="http://static.e-talemedia.net/content/images/Basketmessage.png"
alt="Added to Basket" />
</p>
<% } %>
</div>
<% } %>
<%-- This is the quantity--%>
<div class="qty">
<button id="delete" class="minusQty">
</button>
<div id="theCount" class="theCount">
</div>
<button id="addMe" class="addQty">
</button>
</div>
<% } %>
</ul>
</div>
</div>
<%= Html.WidgetImpressionCode(Model) %>
<%= Html.WidgetTrackingCode(Model) %>
以下内容:
<%= Html.WidgetImpressionCode(Model) %>
转到小部件助手,省略了脚本,如下所示:
<input id="e-taleWidgetImpressionId" name="e-taleWidgetImpressionId" type="hidden" value="" />
<script type="text/javascript">
//<![CDATA[
$(document).ready(function () {
var manufacturerWidgetId = "260";
var productId = "6572";
var countryId = "230";
var theUrl = "http://localhost:1579/Widgets/Impression/260/6572/230/9r22r45r80";
$.getJSON(theUrl + "?callback=?", {}, callback);
function callback(data) {
var widgetImpressionId = data.Id;
$("input[name='e-taleWidgetImpressionId']").val(widgetImpressionId);
}
});
//]]>
</script>
我需要在ajax帖子中包含隐藏字段的值
<input id="e-taleWidgetImpressionId" name="e-taleWidgetImpressionId" type="hidden" value="" />
这样我就可以在控制器中使用它了:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OneBySevenBin2(String modelNo, string widgetImpressionId, string page, FormCollection form)
{
...
}
答案 0 :(得分:1)
问题中你的ajax post
代码在哪里? getJSON
使用GET
请求,您可以添加隐藏的字段数据,如
$.ajax({
url:'',
type:'',
data:{hdnField:$("#e-taleWidgetImpressionId").val()},
...
});
在服务器端,它可以被收到
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OneBySevenBin2(String modelNo, string widgetImpressionId, string page, FormCollection form,string hdnField)
{
...
}