0

Is it possible to accomplish something like this?

<script type="text/javascript">
function getValues( keysArray ) {
    var valuesArray = new Array(keysArray.length);
    for (var i = 0; i < keysArray.length; i++) {
        valuesArray[i] = @this.LocalResources(keysArray[i]);
    }
    return valuesArray;
}
1
  • What kind of resource are you attempting to request from the server? Commented Oct 2, 2013 at 19:42

2 Answers 2

2

getValues function will get executed on the browser. Razor is going to execute before the page is send to the browser. there for this wouldn't work.

If you wanted to call the LocalResources method on the server you could to expose a controller action and perform a ajax request from the client.

Perhaps something like this on the browser:

Javascript

 function getValues( keysArray ) {
 $.ajax({
   url: "/Controller/getValues",
   type: "POST",
   dataType: "json",
   contentType: "application/json; charset=utf-8",
   async: true,
   data: JSON.stringify({ "keysArray" : keysArray }),
   success: function (result) {
       //result obj is your array of resources
 }

});

MVC Controller

public JSONResult getValues(object keysArray)
{
    ///Build respurce array here
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the explanation and code sample, this seems to be exactly what I need. Would I then get the resource with HttpContext.GetLocalResourceObject()? What would be my virtual path for it if my resource file lies in localhost:123456/Views/Shared/Asd/App_LocalResources/MyPage.cshtml.resx?
Certainly. Your Server side call to get each resource would look something like HttpContext.GetLocalResourceObject("~/Views/Shared/Asd/App_LocalResources/MyPage.cshtml", KeyString)
I keep getting this error: The resource class for this page was not found. Please check if the resource file exists and try again. (whether or not there's a .resx at the end of the path or not).
Shooting from the hip here but looking at this page the virtual path might be /Views/Shared/Asd/App_LocalResources/MyPage.cshtml.resx remove ~
Doesn't work unfortunately. Tried removing the .resx along with the ~ but to no avail, though the words for this page in the error message is somewhat confusing...
1
  • Ajax request, or;
  • loading the list of necessary Resources keys in page and then accessing them by JS.

3 Comments

+1: sending all resources strings to page is useful option (i.e. if client side code needs more than just on particular string at time).
I'm having trouble using GetLocalResourceObject so I would like to try the second method, though I'm unsure of how to accomplish it. By 'loading the list in page', do you mean something like putting them inside hidden spans and such then grabbing their inner HTML/accessing their IDs when needed?
I mean putting it in a JavaScript object/array-2-dim (key/value) or hidden fields separating by delimiter

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.