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!

5
  • you can't mix server-side and client-side script like that. You'll likely have to use ajax for that. Commented Sep 24, 2012 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. Commented Sep 24, 2012 at 18:12
  • 1
    Please look into this article: [get value from @Model inside jquery script][1] Hope this helps [1]: stackoverflow.com/questions/5770578/… Commented Sep 24, 2012 at 18:14
  • I've found Ajax to be the best solution. Commented Sep 24, 2012 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!! Commented Sep 24, 2012 at 18:33

2 Answers 2

0

You can't!

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.