Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access MVC PartialView Model in Javascript?

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?

like image 858
Mostafa Avatar asked Mar 06 '26 14:03

Mostafa


1 Answers

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>
like image 198
Dean.DePue Avatar answered Mar 08 '26 04:03

Dean.DePue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!