Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

735 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