Reputation: 97
I want to pass the value i got it from model to the java-script function
<script type="text/javascript">
var checkin = @Model.Parameters.Checkin.ToString("dd-MM-yyyy");
var checkout = @Model.Parameters.Checkout.ToString("dd-MM-yyyy");
</script>
this function that i want to pass model chick-in and chick-out value to it:
$('document').ready(function () {
$("#Arrival").val(checkin);
$("#Departure").val(checkout);
});
i tried many solution but it didn't work yet . any advice, thanks
Upvotes: 2
Views: 5044
Reputation: 5284
if the @Model.Parameters.Checkin
and @Model.Parameters.Checkout
not null
then Try:
<script type="text/javascript">
$( document ).ready(function(){
var checkin = '@Model.Parameters.Checkin.ToString("dd-MM-yyyy")';
var checkout = '@Model.Parameters.Checkout.ToString("dd-MM-yyyy")';
$("#Arrival").val(checkin);
$("#Departure").val(checkout);
});
Just you miss '
. and also change $('document').ready(function () { })
to $(document).ready(function () { })
.
you must write all script
into a .cshtml
file. @Model.Parameters.Checkin.ToString("dd-MM-yyyy")
never work into a .js
file.
Because, In .cshtml
, when the the page is render then it white to HTTP response stream as a string.
Upvotes: 8
Reputation: 21
In MVC, you can use following code:
<script type="text/javascript">
var number = parseInt(@ViewBag.Number); //Accessing the number from the ViewBag
alert("Number is: " + number);
var model = @Html.Raw(@ViewBag.FooObj); //Accessing the Json Object from ViewBag
alert("Text is: " + model.Text);
</script>
Upvotes: 0