0

In a razor page, I'd like assign a javascript variable from a model property (not all the model)

I tried several solution but no one works.

This code is plaed at the end of the razor page. The value I get for

<script>

    $(document).ready(function () {

        var method = @Model.MethodName;

        if (!methodd)
            alert(method);
    );

</script>

I tried this too :

<script>

    $(document).ready(function () {

        var mName = '';

        @if(@Model.MethodName != string.Empty)
            mName = @Model.MethodName;

        alert(mName);

    });

</script>

In the console I get :

var method = ;

Any idea ?

Thanks,

2
  • If this is a string property, you need something like "@Model.MethodName" Commented Jul 27, 2022 at 9:20
  • @adiga Of course I have this property in the model Commented Jul 27, 2022 at 11:54

1 Answer 1

-1

A solution would be to use JSON.Encode() method.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.