Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

If I hide a div with Jquery with hide function, how will I get the value of the fields inside it of textboxes when form is posted to controller action method? The div is hidden but I need the value when the form is posted to the action method. Kindly help friends.
Posted
Comments
Is that textbox bind with model property?
Afzaal Ahmad Zeeshan 9-Jun-15 15:00pm    
You cannot pass values from a div, but from a form. Is that div containing a form or something like that?

1 solution

You can use div in the front-end for hide or show controls within your page, for posting data to the controller you can use hidden fields which are never visible.
C#
@using (Html.BeginForm("ControllerAction", "ControllerName", FormMethod.Post, new { @class  "form-horizontal container" }))
{
 @Html.TextBoxFor(x => x.ModelProperty,     //you can define the model field as hidden
      {
     new  @type = "hidden"
})	

//you can also define the field as hidden 
<input type="hidden" class="form-control" name="FieldName" id="FieldID" value="@Model.PropertyName" />			


//the button that will submit the form to the controller

<input type="submit" class="btn btn-success right" value="Submit" id="btnSubmit">
}

In this way the hidden fields will be submitted to controller along with other fields.
Note: you can also post data from the form which are not bounded to the model, then you need to send the field names to the controller to grab the data.

Hope this will help.
 
Share this answer
 
v3
Comments
sam_roy 10-Jun-15 5:19am    
Its a hidden div like <div id="contractor-first" class="row break col-xs-10 col-xs-offset-1 " style="display: none;"> inside that i have form as <form id="profile-form" class="form-horizontal" method="post" action="/partner/register" novalidate="novalidate"> inside that i have <input id="FirstName" class="form-control " type="text" value="" placeholder="" name="FirstName" data-val-required="Please enter the First Name" data-val-maxlength-max="100" data-val-maxlength="Please enter 100 or fewer characters" data-val="true"> i need its value in controller. It is strongly bind with a class. How would I get the value in form post in controller post method?
Mostafa Asaduzzaman 10-Jun-15 17:42pm    
read the field value within a javascript function and with the button click execute the function to post the field value to the controller.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900