maztt
maztt

Reputation: 12294

asp.net mvc jquery hidden field

the thing is that I have a form with a textbox and a button and a hidden field, now what i want to do is to write something in the textbox ,pass it to the hidden field and then access that thing written in the hidden field in the controller . how can i do that?

Upvotes: 0

Views: 3228

Answers (3)

SAHIL SINGLA
SAHIL SINGLA

Reputation: 755

Why a hidden field?

Why can't you simply pass the Value of the TextField using jQuery's OnButtonClick handler?

Below is the JavaScript code:-

$(document).ready(function(){
$("#Create").click(function(){
var data1 = $('#TextField').val();

 $.ajax({
         type:"Post",
         url:'/Controller/SomeFunction',
         data:"Name="+ data1,
         success:function(result)
         {
            alert(result);
         },
         error:function(result)
         {
            alert("fail");
         }
     }); 
   });
});

Upvotes: 3

msi
msi

Reputation: 3212

You can use OnSubmit form event, and javascript to copy data from TextBox to the hidden field. Here is a simple html exmple:

<html>

<head>
<script type="text/javascript">
function AddDataToHidden()
{
  document.getElementById('test').value = document.getElementById('login').value;
}
</script>
</head>

<body>
  <form action="action.aspx" onsubmit="javascript:return AddDataToHidden();" method="get">
    <input type="text" id="login" name="login" />
    <input type="hidden" id="test" name="test" />
    <input type="submit" />
  </form>
</body>
</html>

Upvotes: 3

Artiom Chilaru
Artiom Chilaru

Reputation: 12201

Why do you need a hidden field for that? If you have a textbox, then on form submit, the value from the textbox will be directly available.

I might not understand you correctly, but from what it looks, your textbox's value will always have the same value as the hidden one..

What exactly are you trying to accomplish then?

Upvotes: 1

Related Questions