Solved

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

Posted on 2012-03-22
4
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:binaryevo
binaryevo earned 500 total points
ID: 37754691
0
 

Author Comment

by:newbieweb
ID: 37754943
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
ID: 37757245
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
ID: 37797116
Thanks. Sorry for the delay...
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

726 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