Reputation: 1011
I need to add a value from a hidden field into my Ajax post.
I'm afraid i do not know a lot about Ajax so I will try to explain what i need:
In my view page I have the following:
<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) %>
The following:
<%= Html.WidgetImpressionCode(Model) %>
Goes to a widget helper which omits a script as follows:
<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>
I need to include the value of the hidden field in the ajax post
<input id="e-taleWidgetImpressionId" name="e-taleWidgetImpressionId" type="hidden" value="" />
This is so I can use it in the controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OneBySevenBin2(String modelNo, string widgetImpressionId, string page, FormCollection form)
{
...
}
Upvotes: 0
Views: 159
Reputation: 2620
where is your ajax post
code in the question? getJSON
uses the GET
request any way you can add the hidden field data like
$.ajax({
url:'',
type:'',
data:{hdnField:$("#e-taleWidgetImpressionId").val()},
...
});
on the server side it could be recieved as
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OneBySevenBin2(String modelNo, string widgetImpressionId, string page, FormCollection form,string hdnField)
{
...
}
Upvotes: 1