pseudoware
pseudoware

Reputation: 49

In razor view, how to pass model property to JavaScript function and display return value?

I have a partial view that is rendered multiple times from a foreach loop in the parent view.

I need to take a model property value (string), pass it to a javascript function to do some checking and possible processing, string manipulation etc., and then return the value to the razor view for display, likely using @Html.Raw.

Can anyone suggest how to accomplish this, and is this the appropriate approach for this problem (this has to be done on the client)? Thank you.

Upvotes: 0

Views: 1915

Answers (1)

Bubavanhalen
Bubavanhalen

Reputation: 148

Im not sure but, you can try something like this!

<script>
$(function() {
    var jsVar = @Model.Text; //It works, just the editor shows an error.

    //Do your Stuff with jsVar;

    $('#txtExample').prop('value', jsVar); //write it back!
});
</script>

@Html.TextBoxFor(e => e.Text, new {id = "txtExample"})

Upvotes: 2

Related Questions