How can jQuery use a model?
@using(Html.BeginForm()) { <fieldset> <p> <label for="Title">Referans Adı</label> <p> @Html.TextBoxFor(Model=> Model.Name) </p> How can these models be used in javascript, as in an html form?
Can javascript use model data like used in html for example? (e.g. Model.Name)
Yes. Razor may only analyze the following penalties:
<script type="text/javascript"> $(document).ready(function() { alert("@Model.Name"); } </script> If you want to save javascript in a separate .js file, and not in your view, you can use a hidden field to store the value.
<input type="hidden" id="name-hidden" value="@Model.Name" /> Then you can access it in javascript using the id. For example, in jQuery:
$(document).ready({ var $nameHidden = $('#name-hidden'); alert('Old Name: ' + $nameHidden.val()); $nameHidden.val('Some New Name'); alert('New Name: ' + $nameHidden.val()); }); Some people prefer that you don’t actually do it this way and instead load your model as JSON and use these javascsript elements directly in the script syntax rather than in a razor. The razor parser sometimes throws errors during compilation / design (not at runtime, since I believe this is just intellisense related) when you include @whatever inside the block.
for those of us who are still using .aspx in some old projects:
alert("<%= Model.OrderStatus %>");