0

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.

6
  • check this stackoverflow.com/questions/227624/… Commented May 5, 2014 at 5:52
  • Thanks. My sol'n has to be done entirely on the client. Nothing server-side. Commented May 5, 2014 at 5:56
  • 2
    Razor does not execute on the client side, it generates the static html to send to the client. You'll need to update an element in the DOM with the result of the Javascript function. Have a look at a Javascript library such jQuery to facilitate this. jQuery.val() is probably the function you want. Commented May 5, 2014 at 6:02
  • When you pass your model to the view, which checking do you do on the client side? You must think that if this checking can affect the view it is better to let the controller do all the job, the view must just render the model passed. Commented May 5, 2014 at 6:07
  • show some of your code Commented May 5, 2014 at 6:09

1 Answer 1

2

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"})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.