I Have a hidden field that contains a value and my code block within view (within @{} ) requires the same . I have tried a lot of formats trying to use jquery with @: etc. but non of them work.
so basically if i have a hidden field with ID=”aField” having a value, I will require the value of it somehow on to my say a string variable.
@{ string hiddenValue = //Value from that hidden field }
Thanks,
Anand
UPDATE :
The value onto hidden field is populated on some client actions using Jquery and is required before form submit. Hence I am not using view bag . Am i mistaken or missing something ?
hence i am looking for a way to may be if possible use jquery to read back the updated value and use it .
tried a lot to include jquery within the above shown code block to get the value but always end up in errors.
UPDATE 2:
Implemented the suggestion by Shoib (thanks for that) to use Ajax and update the same in viewbag and use it for my requirement, also with break points I see that ViewBag has updated value that I am using in my form.
the next thing i am stumbled up is , on browser still i do not see the value selected . it just displayes old value. Is my update lost during rendering / is it just rendering old HTML ? Any help appreciated
below is my script. server side i just update ViewBAg property with this value that works confirmed.
function setHiddenFieldValue(buttonEditClicked)
{
$('#contactIdHiddenFld').val("");
$('#contactIdHiddenFld').val(buttonEditClicked.title) ;
alert($('#contactIdHiddenFld').val());
$.ajax({
url: "@Url.Action("SetPartnerProfileContactID", "PartnerManagement")",
data: {partnerContactID: $("#contactIdHiddenFld").val()},
dataType: 'html',
cache: false,
type: "POST",
error: function() {
alert("An error occurred.");
},
success: function(result) {
jQuery.validator.unobtrusive.parse('EditContactForm');
Sys.Mvc.FormContext._Application_Load();
var windowElement = $('#EditContactWindow');
windowElement.data('tWindow').center().open();
}
});
Also here is a snippet of the form .
@using (Html.BeginForm("GetProfile", "PeopleManagement", FormMethod.Post, new { id = "EditContactForm" }))
{
<p class="note">
Please Enter the contact details below</p>
<table border="0" cellpadding="0" cellspacing="0" id="EditContactsTable">
@{
string toCheck = ViewBag.ContactIDToEdit;
int count = (from n in Model.PartnerDetails.Contacts where n.PrimaryContactId==toCheck select n).Count();
}
<tr>
<td class="table_label_cell" width="20%" >
<font color="red">*</font><label for="firstname">@ViewResources.PartnerManagement.GetPartnerProfile.FirstName :</label>
</td>
<td class="table_value_cell" width="40%">
@Html.TextBoxFor(m => m.PartnerDetails.Contacts[count].PrimaryContacts.FirstName)
</td>
<td class="table_value_cell" width="40%">
@Html.ValidationMessageFor(e => e.PartnerDetails.Contacts[count].PrimaryContacts.FirstName)
</td>
</tr>
<tr>
<td class="table_label_cell" width="20%">
<font color="red">*</font><label for="lastname">@ViewResources.PartnerManagement.GetPartnerProfile.LastName :</label>
</td>
<td class="table_value_cell" width="40%">
@Html.TextBoxFor(m => m.PartnerDetails.Contacts[count].PrimaryContacts.LastName)
</td>
<td class="table_value_cell" width="40%">
@Html.ValidationMessageFor(e => e.PartnerDetails.Contacts[count].PrimaryContacts.LastName)
</td>
</tr>
this way you can access it in view.
for more details on viewbag and other options:
http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications
Regards.