4

For accessing Model of View by Javascript I can use

var additional = '@Html.Raw(Json.Encode(Model))';

But how about Model of Partial view from the main view?

The problem I'm facing is I load partial view into a Keno UI window widget via jQuery ajax call, Inside the partial view I have Upload widget and it has some event ( ie: onUpload event ) , if i put the onUpload event inside the partialview, it doesn't recognize it. So I had to put it in Main view.

In that case '@Html.Raw(Json.Encode(Model))' return the Model of Main view and not the partial one.

Any ideas on how to resolve this problem?

2
  • How do you load in the partialview ? Via a JavaScript ajax call? or is it via a @{ Html.RenderAction("YourView"); } Commented Jun 4, 2015 at 16:06
  • @scgough , I load via jquery ajax call Commented Jun 4, 2015 at 16:11

2 Answers 2

1

This is the way I have access to all of the model:

 var model = function () { return @Html.Raw(Json.Encode(Model)) }();

The partial view has a model:

  @model SomeModel


  <script type="text/javascript">
     $(document).ready(function() {
        var model = function () { return @Html.Raw(Json.Encode(Model)) }();
      });
   </script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Dean , But as mentioned in the question , it return the model of main view and not the partial.
0

Ok, If you're loading the Partial via an Ajax call and you want to initiate a <script> block within the Partial, you could run the following JS on the successful load of the Ajax call (after you've dropped the Partial's HTML on the parent page).

var reponseScript = $(jqXHR.responseText).filter("script");
jQuery.each(reponseScript, function (idx, val) { eval(val.text); })

I'm assuming you're using jQuery there. It will run any Partial's JavaScript blocks.

2 Comments

Thank you , But what would this code suppose to do ? Does it cause the java script event in partial view fire ? Because as I try it , It doesn't
No it should just 'register' it when the partial is loaded into the main view. Then the event should fire 'onupload' as expected. Obviously depends on the JS code in the partial. The idea is the JS code can stay in the partial view and it won't matter it has been loaded in via Ajax. I've used it a few times.

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.