Solved

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

Posted on 2012-03-22
4
432 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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-…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

772 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