Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

MVC3: How do I reference the model in the JavaScript for a page?

I see I can use the @ sign to do things like Url.Action(). But how do I reference the properties for the Model that the page is strongy typed to?


Thanks.
SOLUTION
Avatar of binaryevo
binaryevo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

From the introduction:

"While JavaScript blocks declared inline on the view page can easily consume values from the model..."

My JavaScript is declared in the MVC3 Razor file thusly:

@section Scripts
{
   @Html.Partial("Parts.Index.Scripts")
}

Does this change anything? I do not relish needing to dig into this JSon project described in the URL you'd attached. Is there an easier way?

For example, is there a built in Html Helper that can return the Model inside the JavaScript?

Something similar to...

function myFunc() {
   var myVal = @Html.SomeHelper(model => model.MyValue);

    or

  var myVal = @HtmlHelper.SomeHelper(model => model.MyValue);

}

Thanks.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. Sorry for the delay...