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.

pseudoware
  • 49
  • 9
  • check this http://stackoverflow.com/questions/227624/asp-net-mvc-controller-actions-that-return-json-or-partial-html – Se0ng11 May 05 '14 at 05:52
  • Thanks. My sol'n has to be done entirely on the client. Nothing server-side. – pseudoware May 05 '14 at 05: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`](http://www.jquery.com/) to facilitate this. [`jQuery.val()`](http://api.jquery.com/val/) is probably the function you want. – Xenolightning May 05 '14 at 06: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. –  May 05 '14 at 06:07
  • show some of your code – Ehsan Sajjad May 05 '14 at 06:09
  • Thanks for the replies. The output I'm trying to manipulate is cached. I'm trying to manipulate it only for some users which is why this s/b done on the client. I tried an HtmlHelper, but the changes are still cached even though that's called from the view. – pseudoware May 05 '14 at 06:46

1 Answers1

1

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"})
Bubavanhalen
  • 128
  • 9