Solved

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

Posted on 2012-03-22
4
429 Views
Last Modified: 2012-04-02
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.
0
Comment
Question by:newbieweb
  • 2
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:binaryevo
binaryevo earned 500 total points
Comment Utility
0
 

Author Comment

by:newbieweb
Comment Utility
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.
0
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
Comment Utility
From my above link:

Place scripts in partial views

Instead of following best-practices and placing JavaScript in an external script file, scripts can instead be placed in partial views.  This simplifies the main view by encapsulating the script, and it does allow the script to be re-used.  It also allows the script to easily reference values from the view model.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AwesomeViewModel>" %>
<script type="text/javascript">
 
alert('Hello from the view model: <%=Model.Hello%>');
 
</script>

Open in new window

0
 

Author Closing Comment

by:newbieweb
Comment Utility
Thanks. Sorry for the delay...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now