Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assign a value to a Html.Hiddenfor from jQuery/ JavaScript?

I have a hidden asp.net MVC control in the form:

<%= Html.HiddenFor(m => m.NodeId) %>

My JavaScript / jQuery code:

var DeleteEntireItem = '<% = btnDeleteEntireMenu.ClientID%>';
var Node;
debugger;

$('#' + DeleteEntireItem).click(function () {
    Node = NodeValue;
    document.forms[0].submit();
});

How can I assign the value of variable 'Node' to the asp.net MVC hidden control?

like image 971
SRA Avatar asked Jul 13 '10 06:07

SRA


People also ask

How does HTML HiddenFor work?

HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, IDictionary<String,Object>) Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.


1 Answers

You can set the value attribute of a hidden input tag by selecting it using the property name and using the jquery val() method.

In your case this becomes:

$("#NodeId").val(Node)
like image 98
Stefaan Avatar answered Oct 20 '22 08:10

Stefaan