Yoda
Yoda

Reputation: 18068

How to access model property inside java script function

I want to display prompt for user to make easy for him to copy some text which would be concatenation of Model properties.

If I do this:

   function CopyToClipboard() {        
        window.prompt("Copy to clipboard: Ctrl+C, Enter", @Model.Id);
    }

it works I get:

enter image description here

but If I want to pass property of type String, like this:

   function CopyToClipboard() {        
        window.prompt("Copy to clipboard: Ctrl+C, Enter", @Model.FirstName);
    }

there is no prompt.

Upvotes: 1

Views: 109

Answers (2)

Santiago
Santiago

Reputation: 56

Try

function CopyToClipboard() {        
    window.prompt("Copy to clipboard: Ctrl+C, Enter", '@Model.FirstName');
}

The javascript code has to be inside the html receiving the model.

Upvotes: 0

adeneo
adeneo

Reputation: 318352

The number 2 can be inserted without quotes, and is converted to a string automagically

function CopyToClipboard() {        
    window.prompt("Copy to clipboard: Ctrl+C, Enter", 2);
}

any other string would be a syntax error

function CopyToClipboard() {        
    window.prompt("Copy to clipboard: Ctrl+C, Enter", Yoda); // error
}

because it needs to be quoted

function CopyToClipboard() {        
    window.prompt("Copy to clipboard: Ctrl+C, Enter", "@Model.FirstName");
}

Upvotes: 2

Related Questions