Bart Hofma
Bart Hofma

Reputation: 644

How to access a controller variable from the view

I started experimenting with C# and HTML5 today. I thought this question would be easy to find an answer to, but I couldn't find it on Google or here. I created a MVC4 default template. I have an ascx file, which is my form view. It's currently like this

<hgroup class="title">
<h1>Log in.</h1>
<h2>Enter username and password</h2>

But instead of a hardcoded "Log in." I want to put a localization String there from another class called HomeController.cs

So I thought I'd do it like this. (Which I'm used to doing because so far I only know Java)

<hgroup class="title">
<h1><script>HomeController.strings.voerIn</script></h1>
<h2>Enter username and password</h2>
</hgroup>

However, that just shows the variable name as text instead of the variable value. I tried wrapping those variables in script> or code> but that doesn't seem to help.

EDIT: ANSWER

Okay, so i finally got it to work. I had to instantiate HomeController, and instead of <% %> displaying the string I had to use <%= %>

so this is what my code looks like now, and it works!!

<% MvcApplication2.Controllers.HomeController hc=new MvcApplication2.Controllers.HomeController();%>
  <h2><%=hc.aha%></h2>

Upvotes: 0

Views: 161

Answers (1)

Pierre Pellegrino
Pierre Pellegrino

Reputation: 406

To put code in your page, you must use <% %>.

Upvotes: 3

Related Questions