2

I’m trying to embed a JQuery string within a Razor object that access it's property....something like this:

var propertyID = $(this).attr('id');  
var modelData = “@Model.InitialHistory.” + propertyID;  

modelData should look something like this: @Model.InitialHistory.Property_1;

(if i type @Model.InitialHistory.Property_1; in my code it works fine, just need to dynamically select the razor property)

is something like this even possible or am I going to have to create an ajax call?

thanks!

miguel
  • 81
  • 4
  • you can't mix server-side and client-side script like that. You'll likely have to use ajax for that. – MrOBrian Sep 24 '12 at 18:10
  • You should put all the data from your razor object into (a) javascript variable(s). Then dynamically select it client-side, or use AJAX like you mention above. JavaScript cannot interact with Razor directly. – Joshua Enfield Sep 24 '12 at 18:12
  • 1
    Please look into this article: [get value from @Model inside jquery script][1] Hope this helps [1]: http://stackoverflow.com/questions/5770578/get-value-from-model-inside-jquery-script – Display Name Sep 24 '12 at 18:14
  • I've found Ajax to be the best solution. – samuelesque Sep 24 '12 at 18:23
  • Oh wow....didn't think of that, putting the @Model in a JQuery object - great idea! However, I tried that and got this error: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies......" At this point it seems to me making an Ajax call is probably a better "solution" (the Model I have has about 150+ columns of data which leaves more room for error when serializing to JSON) much thanks for your help!! – miguel Sep 24 '12 at 18:33

2 Answers2

0

You can't!

javascript-jQuery is client side scripting, while razor is server side code.

gdoron
  • 147,333
  • 58
  • 291
  • 367
0

I would just make Model.InitialHistory a list, and loop through that in the view.

Kaizen Programmer
  • 3,798
  • 1
  • 14
  • 30