How do I access the PartialView MVC model in Javascript?

To access the view model via Javascript, I can use

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

      

But what about the Partial View Model from the main view?

The problem I am facing is that I am loading a partial view to the Keno window widget via jQuery ajax call, inside the partial view Load widget and it has some kind of event (i.e. event onUpload

) if I put the event onUpload

inside a partial view, admit it. So I had to put it in the Main view.

In this case, it '@Html.Raw(Json.Encode(Model))'

returns the main view, not the partial.

Any ideas on how to resolve this issue?

+3


source to share


2 answers


This is how I have access to the entire model:

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

      



The model has a partial view:

  @model SomeModel


  <script type="text/javascript">
     $(document).ready(function() {
        var model = function () { return @Html.Raw(Json.Encode(Model)) }();
      });
   </script>

      

+1


source


Ok, If you are loading Partial with an Ajax call and want to initiate a block <script>

inside Partial, you can run the following JS on successful loading of the Ajax call (after you have dumped the partial HTML to the parent page).

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

      



I am assuming you are using jQuery. It will run any JavaScript partial blocks.

0


source







All Articles