6

<%: Html.HiddenFor(model => model.Name) %>


<script> 
var name = <%: Model.Name %> 
alert(name);
</script> 

1 Answer 1

10

You need to put quotes around the value so that it is considered as a string:

var name = '<%: Model.Name %>';
alert(name);

But if you already have the value inside a hidden field:

<%: Html.HiddenFor(model => model.Name) %>

you could read it like this:

var name = document.getElementById('Name').value; // make sure the id is Name
alert(name);

or using jquery:

var name = $('#Name').val();
alert(name);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.