RememberME
RememberME

Reputation: 2092

How to access view model data in jquery?

In my html, I use Model.Subcontract.company

How can I reference that data in my jQuery?

Upvotes: 1

Views: 15233

Answers (3)

m00seDrip
m00seDrip

Reputation: 4021

This is an effective way to reference the model as an object in jQuery:

var model = @Html.Raw(Json.Encode(Model))

If you want to avoid the harmless, intellisense error:

var model = [@Html.Raw(Json.Encode(Model))][0];

Upvotes: 1

RememberME
RememberME

Reputation: 2092

I was able to do it like this:

"<%= Model.Subcontract.company %>"

It's always easier to figure it out after you've given up and asked the question!

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38503

Set the value to an html element and then access it via jquery.

<input type="hidden" value="<%= Model.Subcontract.Company %>" id="hidData" name="hidData" />

$("#hidData").val();

Upvotes: 1

Related Questions