Talha
Talha

Reputation: 19242

Unable to access property using MVC3 Razor

I want to access the property

public string gridHTML { get; set; }

that I have defined in my controller. I am trying to access this property using the JavaScript, MVC3 Razor like this

$(document).ready(function() {

   var str = @Model.gridHTML ;
}

but i am getting the error that the above variable is not defined. Can anybody help me. Thanks in advance.

Upvotes: 0

Views: 226

Answers (2)

jim tollan
jim tollan

Reputation: 22485

Talha,

In the comments to 3nigma's question, you make an interesting note:

does not contain a definition for 'gridHTML' and no extension method 'gridHTML' accepting a first argument of type 'System.Collections.Generic.IEnumerable

are you certain that you are passing a single object to your view?? It sounds to me as tho you are using an ienumerable list that contains your class. Perhaps a paste of both the class and the razor view header section would tease your question out a little further.

Upvotes: 1

Rafay
Rafay

Reputation: 31033

if you have passed the model correctly then the following should work, unless the razor syntax is not residing inside a separate js file

$(document).ready(function() {

   var str = '@:Model.gridHTML';
   console.log(str);
}

Upvotes: 1

Related Questions